首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算时差超过24小时

计算时差超过24小时
EN

Stack Overflow用户
提问于 2016-10-04 14:57:38
回答 1查看 604关注 0票数 2

我有一个问题,我试图计算时差的秒,然后在一个报告(访问报告),我将总结这些秒,并将其格式化为hh:nn:ss。

然而,我的计算场,收集两个字段之间的时差,有时超过24小时,从而抛出时差。

我使用的是DateDiff函数-- DateDiff("s",BeginningTime,EndingTime)

在时间超过24小时的情况下,我该怎么办?

这两个字段,BeginningTime和EndingTime,以AM/PM格式存储。不过,我不认为那应该重要。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-04 15:55:47

您可以使用这样的函数:

代码语言:javascript
复制
Public Function FormatHourMinute( _
  ByVal datTime As Date, _
  Optional ByVal strSeparator As String = ":") _
  As String

' Returns count of days, hours and minutes of datTime
' converted to hours and minutes as a formatted string
' with an optional choice of time separator.
'
' Example:
'   datTime: #10:03# + #20:01#
'   returns: 30:04
'
' 2005-02-05. Cactus Data ApS, CPH.

  Dim strHour       As String
  Dim strMinute     As String
  Dim strHourMinute As String

  strHour = CStr(Fix(datTime) * 24 + Hour(datTime))
  ' Add leading zero to minute count when needed.
  strMinute = Right("0" & CStr(Minute(datTime)), 2)
  strHourMinute = strHour & strSeparator & strMinute

  FormatHourMinute = strHourMinute

End Function

这个表达式作为文本框的ControlSource:

代码语言:javascript
复制
=FormatHourMinute([EndingTime]-[BeginningTime])

但是(请参阅注释)这个简单的表达式只对数值为正的日期有效,这些日期是1899-12-30之后的日期。

要涵盖所有日期,您需要一种计算时间周期的适当方法,这可以使用以下函数来完成:

代码语言:javascript
复制
' Converts a date value to a timespan value.
' Useful only for date values prior to 1899-12-30 as
' these have a negative numeric value.
'
'   2015-12-15. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DateToTimespan( _
    ByVal Value As Date) _
    As Date

    ConvDateToTimespan Value

    DateToTimespan = Value

End Function


' Converts a date value by reference to a linear timespan value.
' Example:
'
'   Date     Time  Timespan      Date
'   19000101 0000  2             2
'
'   18991231 1800  1,75          1,75
'   18991231 1200  1,5           1,5
'   18991231 0600  1,25          1,25
'   18991231 0000  1             1
'
'   18991230 1800  0,75          0,75
'   18991230 1200  0,5           0,5
'   18991230 0600  0,25          0,25
'   18991230 0000  0             0
'
'   18991229 1800 -0,25         -1,75
'   18991229 1200 -0,5          -1,5
'   18991229 0600 -0,75         -1,25
'   18991229 0000 -1            -1
'
'   18991228 1800 -1,25         -2,75
'   18991228 1200 -1,5          -2,5
'   18991228 0600 -1,75         -2,25
'   18991228 0000 -2            -2
'
'   2015-12-15. Gustav Brock, Cactus Data ApS, CPH.
'
Public Sub ConvDateToTimespan( _
    ByRef Value As Date)

    Dim DatePart    As Double
    Dim TimePart    As Double

    If Value < 0 Then
        ' Get date (integer) part of Value shifted one day
        ' if a time part is present as -Int() rounds up.
        DatePart = -Int(-Value)
        ' Retrieve and reverse time (decimal) part.
        TimePart = DatePart - Value
        ' Assemble date and time part to return a timespan value.
        Value = CDate(DatePart + TimePart)
    Else
        ' Positive date values are identical to timespan values by design.
    End If

End Sub

然后,您的表达式将如下所示:

代码语言:javascript
复制
=FormatHourMinute(DateToTimespan([EndingTime])-DateToTimespan([BeginningTime]))

对于Gord的示例值,#1899-12-28 01:00:00##1899-12-27 23:00:00#将返回2:00。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39855601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档