如何减去日期和时间数组以获得 Excel VBA 中的分钟差值?
How to subtract Date and Time Arrays to get a difference in Minutes in Excel VBA?
我有两个字符串数组,其中包含按以下顺序排列的值:
日期(年-月-日)和时间(时-分-秒)。
代码如下:
sDateSplitArray1 = Split(sDate1, "-") ' sDateSplitArray1(1)=Year, ...(2)=Month, ...(3)=Day
sTimeSplitArray1 = Split(sTime1, "-") ' sTimeSplitArray1(1)=Hour, ...(2)=Minutes, ...(3)=Seconds
sDateSplitArray2 = Split(sDate2, "-")
sTimeSplitArray2 = Split(sTime2, "-")
现在如何计算两者之间的绝对差值(以分钟为单位)?
我就是想看看两者之间有没有超过30分钟
Function TimeStamp(ByVal sDate As String, ByVal sTime As String) As Date
' sDate = "yyyy-mm-dd"
' sTime = "hh-mm-ss"
Dim sD, sT
sD = Split(sDate, "-")
sT = Split(sTime, "-")
TimeStamp = DateSerial(sD(0), sD(1), sD(2)) + TimeSerial(sT(0), sT(1), sT(2))
End Function
Function sDiff(sDate1, sTime1, sDate2, sTime2)
Const inMinutes = "n"
Dim d1, d2
d1 = TimeStamp(sDate1, sTime1)
d2 = TimeStamp(sDate2, sTime2)
sDiff = DateDiff(inMinutes, d1, d2)
End Function
我有两个字符串数组,其中包含按以下顺序排列的值: 日期(年-月-日)和时间(时-分-秒)。
代码如下:
sDateSplitArray1 = Split(sDate1, "-") ' sDateSplitArray1(1)=Year, ...(2)=Month, ...(3)=Day
sTimeSplitArray1 = Split(sTime1, "-") ' sTimeSplitArray1(1)=Hour, ...(2)=Minutes, ...(3)=Seconds
sDateSplitArray2 = Split(sDate2, "-")
sTimeSplitArray2 = Split(sTime2, "-")
现在如何计算两者之间的绝对差值(以分钟为单位)? 我就是想看看两者之间有没有超过30分钟
Function TimeStamp(ByVal sDate As String, ByVal sTime As String) As Date
' sDate = "yyyy-mm-dd"
' sTime = "hh-mm-ss"
Dim sD, sT
sD = Split(sDate, "-")
sT = Split(sTime, "-")
TimeStamp = DateSerial(sD(0), sD(1), sD(2)) + TimeSerial(sT(0), sT(1), sT(2))
End Function
Function sDiff(sDate1, sTime1, sDate2, sTime2)
Const inMinutes = "n"
Dim d1, d2
d1 = TimeStamp(sDate1, sTime1)
d2 = TimeStamp(sDate2, sTime2)
sDiff = DateDiff(inMinutes, d1, d2)
End Function