3

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.

jk1844
  • 327
  • 1
  • 5
  • 12

2 Answers2

3

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
adaptun
  • 494
  • 4
  • 11
0

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

jk1844
  • 327
  • 1
  • 5
  • 12
  • Why when you need to find difference in hours, you getting difference in seconds. DATEDIFF [supports](http://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=RSQL_datediff) hours too. – DAiMor Sep 24 '16 at 21:59
  • yes, thanks. I could have got the difference in hours . – jk1844 Sep 25 '16 at 21:37