我是VB Scripting的新手。我有两个日期,date_1和date_2。我需要从date_2中减去date_1,并输出日期格式的差异。
示例:
date_1 = 01-09-2014
date_2 = 08-10-2016
输出最好是:
= date_2 - date_1
= 08-10-2016 - 01-09-2014
= 07-01-0002
最后我需要02年,01个月和07天的输出。
请帮帮我。
非常感谢提前。
答案 0 :(得分:0)
看看下面的代码:
Dim date_1, date_2, l, r(2)
date_1 = "01-09-2014"
date_2 = "08-10-2016"
l = SetLocale(2057) ' UK
Delta 0, Array("yyyy", "m", "d"), Array("years", "months", "days"), r, CDate(date_1), CDate(date_2), False
SetLocale l
MsgBox Join(r, ", ") ' 2 years, 1 months, 7 days
Sub Delta(i, t, n, r, d1, d2, c)
Dim q, d
q = DateDiff(t(i), d1, d2)
If UBound(t) > i Then
Do
d = DateAdd(t(i), q, d1)
Delta i + 1, t, n, r, d, d2, c
If c Then Exit Do
q = q - 1
Loop
End If
c = q >= 0
r(i) = q & " " & n(i)
End Sub
您甚至可以设置日期和时间,并计算差异达到秒:
Dim date_1, date_2, l, r(5)
date_1 = "01-09-2014 10:55:30"
date_2 = "08-10-2016 15:45:10"
l = SetLocale(2057) ' UK
Delta 0, Array("yyyy", "m", "d", "h", "n", "s"), Array("years", "months", "days", "hours", "minutes", "seconds"), r, CDate(date_1), CDate(date_2), False
SetLocale l
MsgBox Join(r, ", ") ' 2 years, 1 months, 7 days, 4 hours, 49 minutes, 40 seconds
Sub Delta(i, t, n, r, d1, d2, c)
Dim q, d
q = DateDiff(t(i), d1, d2)
If UBound(t) > i Then
Do
d = DateAdd(t(i), q, d1)
Delta i + 1, t, n, r, d, d2, c
If c Then Exit Do
q = q - 1
Loop
End If
c = q >= 0
r(i) = q & " " & n(i)
End Sub
答案 1 :(得分:-2)
使用一些string functions,您可以轻松实现目标。
希望这有帮助。
date_1 = InputBox("Enter first Date in format DD-MM-YYYY","Time Difference")
date_2 = InputBox("Enter second Date in format DD-MM-YYYY","Time Difference")
day_differ = Abs(CInt(Left(date_2 , 2)) - CInt(Left(date_1 , 2)))
month_differ = Abs(CInt(Mid(date_2 , 4 , 2)) - CInt(Mid(date_1 , 4 , 2)))
year_differ = Abs(CInt(Right(date_2 , 4)) - CInt(Right(date_1 , 4)))
wscript.echo "Date Difference is " & year_differ & " years, " & month_differ & " months and " & day_differ & " days."