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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How many hours to go?

Status
Not open for further replies.

dduva

Programmer
May 16, 2001
38
US
I am looking for an easy way to find out how much time, hours, minutes, seconds are left until some future date and time? Is there a simple commmand? Thanks in advance.

devin
 
No, but there have been many routines presented in past threads to convert the difference between two DateTime values (it's in seconds) to HH:MM:SS. In fact some have added DD (days) also.

Please check the FAQs and use the Keyword Search tabs above.

Rick
 
You can subtract one datetime from another and it will give you the number of seconds between them

x = DATETIME()
?datetime() - x

now it's just a matter of getting the number of hours, minutes and seconds...

LOCAL tFirstdatetime, tSecondDatetime, nDifference
LOCAL nHours, nMinutes, nSeconds
tFirstdatetime = DATETIME()
tSecondDatetime = {^2004/01/01 12:00 AM}
nDifference = tSecondDatetime - tFirstdatetime

nHours = MAX(INT(nDifference/3600),0)

nMinutes = MAX(INT((nDifference % 3600) / 60),0)

nSeconds = nDifference % 60

?"HOURS: " + TRANSFORM(nHours)
?"MINUTES: " + TRANSFORM(nMinutes)
?"SECONDS: " + TRANSFORM(nSeconds)

*!* Testing
*!* ?nDifference
*!* ?nHours * 3600 + nMinutes * 60 + nSeconds

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
I did try to look up this question but I couldn't seem to get any threads with my search criteria.

Question for slighthaze, how do you get the future date into datetime format?

Thanks
 
dduva,

tSecondDatetime = {^2004/01/01 12:00 AM}

...or if you just had a date and wanted to change it to datetime format you could do something like:

tSecondDatetime = DTOT(MyDate) &&note this date will have a time of 12:00 AM

...or if you wanted to put a date with a specific time:

tSecondDatetime = CTOT(DTOC(MyDate) + " 11:30 AM")


Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
There's also DATETIME( nyear, month, day, hour, minute, second )
 
wgcs,

hmmmm...now why didn't I think of that? So used to Datetime() I seem to forget that there are optional parameters.

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
i found this code somewhere in this forum and modified it to my suit. you can do the same.

**************************************************
function timediffs
parameters bd, bt, ed, et
* where bd = beginning date
* bt = beginning time
* ed = ending date
* et = ending time

hr1 = val(left(bt, 2))
hr2 = val(left(et, 2)) + ((ed - bd) * 24)
mn1 = val(substr(bt, 4, 2))
mn2 = val(substr(et, 4, 2))
sc1 = val(right(bt, 2))
sc2 = val(right(et, 2))
tot1 = (hr1 * 3600) + (mn1 * 60) + sc1
tot2 = (hr2 * 3600) + (mn2 * 60) + sc2

tt = tot2 - tot1

thr = alltrim(str(int(tt / 3600)))
tmn = alltrim(str(int((tt % 3600) / 60)))
tsc = alltrim(str((tt % 3600) % 60))
tdc = padl(alltrim(str(int((val(tmn) / 60) * 100))), 2, "0")
return thr + ' hr' + iif(val(thr) <> 1, 's', '') + ', ' +;
tmn + ' min' + iif(val(tmn) <> 1, 's', '') + ', ' +;
tsc + ' sec' + iif(val(tsc) <> 1, 's', '') + '.' +;
chr(13) + 'Decimal equivalent: ' +;
thr + '.' + tdc + ' hour' + iif(val(tdc) <> 1, 's', '') + '.'
endfunc

**************************************************
function timediffn
parameters bd, bt, ed, et
* where bd = beginning date
* bt = beginning time
* ed = ending date
* et = ending time

hr1 = val(left(bt, 2))
hr2 = val(left(et, 2)) + ((ed - bd) * 24)
mn1 = val(substr(bt, 4, 2))
mn2 = val(substr(et, 4, 2))
sc1 = val(right(bt, 2))
sc2 = val(right(et, 2))
tot1 = (hr1 * 3600) + (mn1 * 60) + sc1
tot2 = (hr2 * 3600) + (mn2 * 60) + sc2

tt = tot2 - tot1

thr = alltrim(str(int(tt / 3600)))
tmn = alltrim(str(int((tt % 3600) / 60)))
tsc = alltrim(str((tt % 3600) % 60))
tdc = padl(alltrim(str(int((val(tmn) / 60) * 100))), 2, &quot;0&quot;)
return val(thr + '.' + tdc)
endfunc


lemme know if this helped.

kilroy [trooper]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top