【问题标题】:Difference time between two string not time of day in vbavba中两个字符串之间的时间差不是一天中的时间
【发布时间】:2017-03-29 07:03:02
【问题描述】:

我没有找到解决这个问题的方法,我有两个这种格式的字符串:

string1 = "23:19:03" (hh:mm:ss)
string2 = "27:08:03" (hh:mm:ss)

我需要计算这两个字符串之间的时间差,比如

string1 = "23:19:03" 
string2 = "27:08:03" 
diff = 3:49:00   (h:m:s)

更新 我找到了解决方案:

请随意使用我的代码并提及我,谢谢

Public Function timediff(ByVal time1 As String, ByVal time2 As String) As String
'Author: © Copyright 2017 Audisio Francesco **************************
'Description: Time diffence betweem two string no time of day!!,the format of this two string is ([h]:mm:ss) like excel function
'             time2 must be greater than time1
Dim ore1, ore2, min1, min2, sec1, sec2, tot1, tot2, tot, ore, apsec, min, sec As Double
Dim aladin1() As String
Dim aladin2() As String
'Dim time1, time2 As String
'genero secondi totali
aladin1 = Split(time1, ":")
    ore1 = Val(aladin1(0)) 'Ore
    ore1 = ore1 * 3600
    min1 = Val(aladin1(1)) 'minuti
    min1 = min1 * 60
    sec1 = Val(aladin1(2)) 'secondi
    sec1 = sec1
aladin2 = Split(time2, ":")
    ore2 = Val(aladin2(0)) 'Ore
    ore2 = ore2 * 3600
    min2 = Val(aladin2(1)) 'minuti
    min2 = min2 * 60
    sec2 = Val(aladin2(2)) 'secondi
    sec2 = sec2
'prendo i totali
tot1 = ore1 + min1 + sec1
tot2 = ore2 + min2 + sec2
tot = tot2 - tot1

ore = Int(tot / 3600)
apsec = tot - (3600 * ore)
min = Int(apsec / 60)
sec = apsec - (min * 60)
result = (ore & ":" & min & ":" & sec)
timediff = result
End Function

用途:

string1 = "23:19:03" 
string2 = "27:08:03"
ore1 = timediff(time1, time2)
ore1 = 3:49:0

【问题讨论】:

  • 将字符串转换为时间,获取差异并将差异格式化为“hh:mm:ss”格式。
  • 我不熟悉时间"27:08:03",有什么新东西吗?
  • 不是一天中的时间,这两个字符串是计数器持续时间,格式为“hh:mm:ss”,@Pratham 无法及时转换,因为不是一天中的时间 =D
  • @ShaiRado - 我认为Excel格式是[h]:mm:ss,可以显示总小时数> 1天。
  • @Joe 是否可以将格式 [h]:mm:ss 像 excel 转换为 vba?是的,如果你把 function = TEXT(D5-D4;"[h]:mm:ss") 你有 3:49:00 作为输出

标签: vba time duration


【解决方案1】:

你可以使用这个辅助函数

Function GetDiffTime(string1 As String, string2 As String) As String
    GetDiffTime = GetTime(GetSeconds(string2) - GetSeconds(string1))
End Function

它又使用以下功能:

Function GetTime(seconds As Long) As String
    Dim rest As Long

    rest = seconds \ 3600
    GetTime = GetTime & Format(rest, "00")
    seconds = seconds - rest * 3600

    rest = seconds \ 60
    GetTime = GetTime & ":" & Format(rest, "00")
    seconds = seconds - rest * 60

    GetTime = GetTime & ":" & Format(seconds, "00")
End Function

Function GetSeconds(strng As String) As Long
    Dim timeParts As Variant
    Dim iPart As Integer

    timeParts = Split(strng, ":")
    For iPart = UBound(timeParts) To LBound(timeParts) Step -1
        GetSeconds = GetSeconds + CInt(timeParts(iPart)) * 60 ^ (UBound(timeParts) - iPart)
    Next
End Function

【讨论】:

  • 不客气。鉴于无法使用内置时间函数,1-60-3600 拆分是唯一的方法。我的解决方案特别关注代码的可重用性和维护性。如果您欣赏并采用它,那么您可能还想将此答案标记为已接受。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-08
  • 1970-01-01
  • 2020-05-01
  • 1970-01-01
相关资源
最近更新 更多