hexagon logo

Difference between 2 dates times in Basic Script.

Anyone know of a way to get the difference between 2 date/times in Basic Script? Similar to the datediff function in VBA.
Parents
  • Thank you

    A datetime is in fact a double, with the date in the integer part and the time in the decimals. So, you just convert your dates to double and do the subtraction, and you will get the number of days (and fractions of days) between the two:

    Sub Main
     d1 = cdbl(dateserial(2015, 2, 1))
     d2 = cdbl(dateserial(2015, 3, 1))
     MsgBox d2-d1
    End Sub
    


    gives the answer 28, while

    Sub Main
     d1 = cdbl(dateserial(2016, 2, 1))
     d2 = cdbl(dateserial(2016, 3, 1))
     MsgBox d2-d1
    End Sub
    


    gives 29, so it seems leap years are also handled.


    Thank you! I'll give that a try.
Reply
  • Thank you

    A datetime is in fact a double, with the date in the integer part and the time in the decimals. So, you just convert your dates to double and do the subtraction, and you will get the number of days (and fractions of days) between the two:

    Sub Main
     d1 = cdbl(dateserial(2015, 2, 1))
     d2 = cdbl(dateserial(2015, 3, 1))
     MsgBox d2-d1
    End Sub
    


    gives the answer 28, while

    Sub Main
     d1 = cdbl(dateserial(2016, 2, 1))
     d2 = cdbl(dateserial(2016, 3, 1))
     MsgBox d2-d1
    End Sub
    


    gives 29, so it seems leap years are also handled.


    Thank you! I'll give that a try.
Children
No Data