Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date/Time Calculation

Status
Not open for further replies.

mikexcell

IS-IT--Management
Sep 25, 2003
8
0
0
US
Does anyone have a routine to calculate the number of hours between two dates and times?
 
I'm working on a similar project and, if I come up with a valid algorithm, I'll post it.

At a high level, this is how I figure it'll work, based on calculating the total number of seconds between the two dates and times:

- Normalize the fromdate and todate into Base dates (the DATE(B) format). Subtract fromdate from todate to get the total number of days elapsed. For each full day, add 86400 to the total number of seconds elapsed.

- For the fromtime, calculate the number of seconds already used for the day (hours % 3600) + (minutes % 60) + seconds, and subtract that value from 86400, giving the number of seconds left until midnight.

- For the totime, calculate the number of seconds already used for the day (same as above, except don't subtract the value from 86400).

- Total all the values as the total seconds elapsed, then calculate back into days/hours/minutes/seconds.
 
This is an simple example to calculate the hours:

/* rexx */
date1 = '12 Oct 2006'
time1 = 9
/* use "date2 = DATE()" for current date */
date2 = '13 Oct 2006'
/* use "time2 = time('H')" for current time */
time2 = 11

date1 = DATE('B',date1)
date2 = DATE('B',date2)
datediff = date2 - date1 - 1
hourdiff = 24-time1+time2
hourdiff = hourdiff + 24*datediff
say hourdiff

Hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top