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!

Doing Time Calculations 1

Status
Not open for further replies.

wadesnj

Programmer
Mar 24, 2001
36
0
0
What is the best way to store time values and do calculations on them? Obviously datetime is best storage type, but can I use just the time portion and do time math, i.e. subtract start time from end time etc. and get hours/minutes?
 
To do time arithmetic I find it easiest to use the full datetime values. They can be subtracted directly, and the result is in seconds.

Jim
 
Well, you can do it (using time()) but you have to do the grunt work.

cTime1 = time()

later in the program

cTime2 = time()


You cannot do this ... cTime2-cTime1 = cTimeDifference

That will not work because the times are nothing but character strings.

Print them out and you will see ... 04:21:35 or something to that effect.

You must then parse out the Hour value (04), the Minute value (21), and if you need it, the seconds value (35).

Then you will have use those values to subtract one value from the other and determine the time difference. Of course, you would create a function to do this then it would be finished for all time. I am sure someone has already created one then you might download but it shouldn't take long to make your own and would be instructional in value.

Good luck.
Don
dond@csrinc.com

 
Actually, it's pretty simple to do subtraction with datetime fields. For example:
Code:
time1={^2001-12-25,05:00:00} && 5am Christmas morning
time2={^2001-12-25,12:35:27} && 12:35pm and 27 seconds
Now you can do simple subtraction:
Code:
diff=time2-time1 && results: 27327 (in seconds)
diffHours=int(diff/3600) && Number of hours
diff=diff%3600 && seconds beyond last hour
diffMinutes=int(diff/60) && Number of minutes
diffSeconds=diff%60
You now have 3 numbers: 7, 35, and 27, representing the hours, minutes, and seconds in difference between the two datetime fields.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top