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

Time claculations 2

Status
Not open for further replies.

itsmystie

Instructor
Dec 9, 2005
47
0
0
Hello!

I need to calculate the number of hours between 2 different times (start time and end time)? Anybody can please help me on how to do that and please give guidance as to what type to declare the 2 fields?

For example, if the 2 times are 1730 and 2000 then i should get 2.5 hrs.

Thanks in advance.
 
FPW does not have a native data type for time so you will have to write all your own arithmetical routines. I would store all the times as seconds-since-midnight because that makes the arithmetic easier. This is especially true if you ever have to deal with the situation where the time period runs over midnight.

Your only problems are that you will have to write conversion routine to convert the user input into seconds for storage and then to convert back again for display.

Geoff Franklin
 
FP's TIME() function returns a character string representing the time of day in a 24 hr (military) format.
Example: "14:07:23"

You can parse this time string into variables separately representing Hr, Min, Seconds and then multiply by the appropriate number of seconds in the unit
Example: mnHr * 3600 -> seconds for the hour portion
mnMin * 60 -> seconds for the min portion
Then add them up to get the time's seconds from midnight.

With two times, you can then calculate the difference in seconds and convert back to Hr:Min:Sec

Another, somewhat more direct, way to get time is to use the SECONDS() function which returns a number (not a character string)
Example: 28348.218 && SECONDS() for "07:52:27"
representing the number of seconds since midnight.

With that number for 2 different times, you can again calculate the difference and convert back to Hr:Min:Sec

Good Luck,


JRB-Bldr
VisionQuest Consulting
Business Analyst & CIO Consulting Services
CIOServices@yahoo.com
 
Hi,

Take a look at this thread184-759476 and also thread184-368039
 
T1=seconds()
.
.
.
T2=seconds()

T=T2-T1 && in sec.

In min. is T/60
In hours is T/3600
 
Have a look at thread 182-1117998 on time functions under Win Xp and possible hibernation issues.

 
Thnks TheRambler and octavian2006 but this is not what I'm looking for.

Basically I'm writing a basic program for calculating payroll. So I have to calculate the daily number of hours between the time the employee comes to work and leaves. For this I have 2 fields in my table: starttime and endtime. I need to do this at the end of each month.

So the solution proposed by JRB-Bldr seems best for me. And I have a query regarding the example he has provided:

Example: 28348.218 && SECONDS() for "07:52:27"

Since the SECONDS() function does not take any parameter how is it possible to get the number of seconds since a user-entered time has elapsed?

And how to convert the difference back to HH:MIN?

Thanks in advance.

 
Since the SECONDS() function does not take any parameter how is it possible to get the number of seconds since a user-entered time has elapsed?
If the user is entering the time as hh:mm then you must calculate the number of seconds since midnight as:
[TT]
[tab]3600*hh + 60*mm
[/TT]
and store that value.
And how to convert the difference back to HH:MIN?
If lnSec is the number of seconds since midnight then divide by 3600 and 60 and use the Int() function to get whole numbers:
[TT]
[tab]hh = Int(lnSec/3600)
[tab]mm = Int((lnSec - (3600 * hh))/60)
[/TT]

Geoff Franklin
 
Since the SECONDS() function does not take any parameter how is it possible to get the number of seconds since a user-entered time has elapsed?

Geoff's answer above is the correct approach to convert user entered time.

However, you might need to take multiple day spans between Time1 and Time2 into account.

To do that you need to also capture the individual Dates in addition to the two Times.

If the dates of the two times span multiple days then you need to first determine how many days are spanned.
example: mnDayDiff = (mdDay2 - mdDay1)

Then by knowing the number of days, you add that difference in seconds (mnDayDiff * 86400) to the later time before computing the difference.

Good Luck,


JRB-Bldr
VisionQuest Consulting
Business Analyst & CIO Consulting Services
CIOServices@yahoo.com
 
Well thanks a lot everybody. I've done what you've told and my program works perfectly. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top