增加数小时,分钟,第二次在VBA中增加天数



我的数据库带有列'startdate'as '01/01/2018 10:00'和'enddate'和'enddate'as '01/01/2018 11:25'。我根据某些标准有多个记录集。

我想用第1个"末端"计算第二个" startdate"之间的差异,而不是将它们汇总为所有差异。

我将示例数据库放在此处

http://sqlfiddle.com/#!9/2cc5ba

我认为使用查询是不可能的,所以我已经开始使用VBA进行此操作。

从下面的链接

使用功能弹出时间的帮助

https://msdn.microsoft.com/en-us/vba/access-vba/articles/calculate-elapsed time

我能够在"小时:分钟:second"

中获得第一行的差异为145:30:00

问题编号:1如何将此字符串更改为日期时间格式。

我尝试过'格式(cdate(caclute_time(," hh:mm:ss"(',但它给了我eroor型的不匹配。

问题编号:2

我该如何添加时间例如。104:40:00(HH:MM:SS( 20:21:00(HH:MM:SS(,结果为125:01:00

您应该始终将时间作为日期,直到要格式化为止。既然您没有,那么您会陷入混乱。

我们需要2个功能,一个可以将日期投入到HH:MM:SS格式,一个将其归还给日期。

Public Function DateToHHMMSS(theDate As Date) As String
    DateToHHMMSS = Int(CSng(theDate * 24)) & ":" & Format(theDate, "nn:ss")
End Function
Public Function HHMMSSToDate(theString As String) As Date
    Dim dateArr As Variant
    dateArr = Split(theString, ":")
    If UBound(dateArr) = 2 Then
        HHMMSSToDate = TimeSerial(dateArr(0), dateArr(1), dateArr(2))
    End If
End Function

使用这些功能,您可以轻松地将这些日期添加在一起,例如:

DateToHHMMSS(HHMMSSToDate("104:40:00") + HHMMSSToDate("20:21:00"))

并将字符串施放到日期,只需使用:

DateToHHMMSS("104:40:00")

只需总结时间差:

Select Sum([DateEnde] - [DateStart])
From MTTR

然后使用这样的函数格式化:

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

这样:

Select 
    *,
    Nz((Select Top 1 T.DateStart 
    From MTTR As T 
    Where T.LFD_NR > MTTR.LFD_NR
    Order By LFD_NR Desc), [DateEnde]) - DateEnde As TimeDiff
From 
    MTTR

将其保存为mttr_diff。

然后:

Select 
    FormatHourMinute(Sum([TimeDiff))
From 
    MTTR_Diff

最新更新