【发布时间】: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 作为输出