Difference between two date time stamp in Intersystems Cache

1.8k views Asked by At

I would like to find out the number of hours and minutes between two date time stamp.

if for example

 sDateTime = 2016-01-01 01:00 
 eDateTime = 2016-01-03 02:30

I would like it to output it as 49:30 (49hours and 30minutes) I am unable to figure a method to work this out.

what I have so far:

    Set oMNOF=##class(MNOF.MNOF).%OpenId(Id)

Set zstartDt=oMNOF.sDateTime 
Set startDt=$PIECE(zstartDt,",",1)          
Set startTime=$PIECE(zstartDt,",",2)    

Set zendDt=oMNOF.eDateTime 
Set endDt=$PIECE(zendDt,",",1)          
Set endTime=$PIECE(zendDt,",",2) 

    set dateDiff=((endDt - startDt))     //2 days 
set timeDiff=(endTime - startTime)    //outputs 5400 seconds

     set d = (dateDiff * 24 * 60 * 60)
set h = ((timeDiff - d) / 60)
set m = timeDiff - (d) - (h * 60)

Thank you for the help.

2

There are 2 answers

4
adaptun On BEST ANSWER

Another option:

USER>set mm=$system.SQL.DATEDIFF("mi","2016-01-02 01:00","2016-01-03 02:30")

USER>write "hours=", mm \ 60
hours=25
USER>write "minutes=", mm # 60
minutes=30
2
jk1844 On

Hi thanks to all for the help. I managed to come up with the below, appreciate if someone can improve on this.

<script language="cache" method="MGetData" arguments="pStartDt:%String,pEndDt:%String,pTimeField:%String" returntype="%Library.String">
set val1="00"

//HOUR: check if length equals 1
if $LENGTH($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/3600))=1{
    //add leading zero
    set val1 ="0"_$SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/3600)

}
else{
    //get without leading zero
    set val1 = $SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/3600)
}

//MINUTES: check if length equals 1
if $LENGTH($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/60) - ($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/3600)*60))=1{

    //add leading zero
    set val2 ="0"_($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/60) - ($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/3600)*60))

}
else{

    //get without leading zero
    set val2 = ($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/60) - ($SYSTEM.SQL.FLOOR($system.SQL.DATEDIFF("ss",pStartDt,pEndDt)/3600)*60))

}

//insert result data into the time field
Write "document.getElementById('"_pTimeField_"').value='"_val1_":"_val2_"';"

//Write "alert('"_val1_"^"_val2_"');"

QUIT 1