Home > Back-end >  How to subtract Date and Time Arrays to get a difference in Minutes in Excel VBA?
How to subtract Date and Time Arrays to get a difference in Minutes in Excel VBA?

Time:05-08

I've got two String-Arrays which contain Values in the following order: Date (Year-Month-Day) and Time (Hours-Minutes-Seconds).

Here's the code:

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, "-")

Now how can I calculate the absolute difference between both in minutes? I just want to check if there are more than 30 minutes between the two.

CodePudding user response:

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
  • Related