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.
  • I want you to know that I've been tempted by this challenge a few times, but it is non-trivial. If I had such an example I would surely give it to you. If every month had the same number of days, it would be simply, but you have to convert each month independently, for both dates, and then find the difference in days. The same is true for years, they are not all the same if you cross a leap year. Its not impossible, I just have not done it yet.
  • 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.
  • Nice job Anders1 !! As often the case, I was looking for the more complex approach, when in fact the better solution was much simpler.
  • I wrote this "code", just using assignments, to measure the time of exe of a prog :
    Those lines at the start :

    ASSIGN/V2=SYSTEMTIME("HH:mm:ss")
    ASSIGN/HOUR1=ELEMENT(1,":",V2)
    ASSIGN/MINUTE1=ELEMENT(2,":",V2)
    ASSIGN/SEC1=ELEMENT(3,":",V2)


    Those lines at the end of the prog :


    ASSIGN/V4=SYSTEMTIME("HH:mm:ss")
    ASSIGN/HOUR2=ELEMENT(1,":",V4)
    ASSIGN/MINUTE2=ELEMENT(2,":",V4)
    ASSIGN/SEC2=ELEMENT(3,":",V4)
    IF/HOUR2<HOUR1
    ASSIGN/HOUR2=HOUR2+24
    END_IF/
    ASSIGN/V5=(HOUR2-HOUR1)*3600+(MINUTE2-MINUTE1)*60+(SEC2-SEC1)
    COMMENT/OPER,NO,FULL SCREEN=NO,AUTO-CONTINUE=NO,
    "Time = "+V5+" seconds"


    It works if the prog is less than 2 days !!!!!Smiley

    For dates, it's more hard, because months are not always the same !!!!!
  • 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.
  • You can add a "runtime" output in the report header. I replaced the stats count block since I don't use it. All my reports tell me how long the execute time was on my program. It helps to be able to walk away and work on something else until the program finishes
  • You can add a "runtime" output in the report header. I replaced the stats count block since I don't use it. All my reports tell me how long the execute time was on my program. It helps to be able to walk away and work on something else until the program finishes


    That would be very useful for us. How is this done? Thanks.
  • Do you still need to know how to set this up?
  • Hi William, if you could tell me it would really help me