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 Calculations?...60min = 1... 1

Status
Not open for further replies.

WhiteTiger

Programmer
Jun 26, 2001
605
0
0
US
How can I convert between two times?...

I want to find out calculations for times like
6:15 to 9:15 for a timesheet program...how would I convert those and calculate them to make come out with 3?

Or have like whatever bla bla hours, to whatever bla bla hours = you worked bla bla.blabla hours

It might be something where your timesheet could be filled out by a logon script that when you logged on, it entered a time, when you logged out, it calculates the time and enter it into your timesheet. Regards,
Anth:cool:ny
----------------------------------------
"You say [red]insanity[/red] like it's a BAD THING!"
 
Nope, that doesn't help...all that does is get date differences and the dates always go by day for some reason...It changed 3 and 4 to a difference of 24...meaning 24 hours...I'm looking for HOURS...there is only a difference of 1 between those 2 numbers! Regards,
Anth:cool:ny
----------------------------------------
"You say [red]insanity[/red] like it's a BAD THING!"
 
Mystring = DateDiff("h", "3:00", "4:00") will return 1. You can also specify 'AM' and 'PM'
 
ok, didn't help much...what about the calculation for the minutes?...if I do 1:00 - 1:30 it returns 30...I need it to return .5, as in half an hour. Regards,
Anth:cool:ny
----------------------------------------
"You say [red]insanity[/red] like it's a BAD THING!"
 
Just expanding on woyler statement. The 'n' denotes minutes.


Text1 = DateDiff("n", "3:00", "3:30") / 60 David Paulson

 
Im not sure of the code for vb but convert the time to mins from midnight then subtract then /

exampl
6:15 pm is
((6 + 12) * 60) + 15 = 1095
6pm + 12 for military time 24hr * 60 min + 15 min

((9 +12) * 60 + 15 = 1275

(1275 - 1095) = 180
(180 / 60) = 3


This is how some of the time clock companies do it

Jim Garry
 
Hmmmmmmmmmmmmmmmmmm,

Date/time (Date Data Type) is just a way of interperting a double value. Once you understand this, the calculations are simple. The INteger portion of the date type is just the number of days since Dec. 30, 1898. The fractional part is the fractional part of the day.

So:
[tab]1/2 day = 12 hours = 0.5
[tab]1/4 day = 6 Hours = 0.25
[tab]1/24 day = 1 Hour = 1/24 = 4.16666666666667E-02
[tab]1/48 Day = 1/2 Hour = 0.5 / 24 = 2.08333333333333E-02
[tab]1/3600 Day = 1 Min. = 1 / 3600 = 2.77777777777778E-04

From which, I ASSUME that you can see any number of ways to do the simple arithmatic.

However one sample will not hurt:
? Cdbl(#3:30 PM#)
0.645833333333333

? CDbl(#4:30 PM#)
0.6875

? (CDbl (#4:30 PM#) - CDbl(#3:30 PM#)) * 24
0.999999999999999


MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
Thanks for trying to help, but I dont think you guys understand...on Moo's statement, I can't just have 30min = .5 because I'm also going to have entries of 32 mins and so forth...

I'm trying to get everything from within the hour working correctly...I already knew about the N in the datediff...didn't help...still gave me wrong thing...

It basically said my date difference from 1:00 to 1:30 was 30 minutes...I already knew that...I'm wanting to convert those 30 minutes into .5 minutes...

It's based for a Timesheet...where I'm doing hours...the way you calculate money with time, isn't by using 30 minutes...but by using .5 equaling half an hour, etc... Regards,
Anth:cool:ny
----------------------------------------
"You say [red]insanity[/red] like it's a BAD THING!"
 
Sorry. You really SHOULD READ the post.

MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
This should get you going:

Public Function TimeDiff(ByVal psTimeOne As String, ByVal psTimeTwo As String) As Double
Dim dHour As Double
Dim dMin As Double

dMin = DateDiff("n", psTimeOne, psTimeTwo)

If dMin > 60 Then
dHour = dHour + (dMin / 60)
TimeDiff = dHour
Else
dMin = dMin
TimeDiff = dMin * 0.01
End If

End Function
 
made a slight modification to woyler's code (in bold)
so that

TimeDiff(#3:30:00 PM#, #3:45:00 PM#)

will return a value of .25

Public Function TimeDiff(ByVal psTimeOne As String, ByVal psTimeTwo As String) As Double
Dim dHour As Double
Dim dMin As Double

dMin = DateDiff("n", psTimeOne, psTimeTwo)

If dMin > 60 Then
dHour = dHour + (dMin / 60)
TimeDiff = dHour
Else
dMin = dMin
'TimeDiff = dMin * 0.01
TimeDiff = (dMin / 60)
End If

End Function
 
See...that's my problem, that code doesn't help either...if I put in 7:30 to 8:00, it returns the result as .3

It isn't .3, it's .5......Hmmm, 60 seconds, 100...*.60?

or wait, Grrr...I can't figure this out! Regards,
Anth:cool:ny
----------------------------------------
"You say [red]insanity[/red] like it's a BAD THING!"
 


I get a value of .5 when I put in 7:30 to 8:00????

(maybe you just haven't seen my post...)
 
Oh, I did quite different modification...

Public Function TimeDiff(ByVal psTimeOne As String, ByVal psTimeTwo As String) As Double
Dim dHour As Double
Dim dMin As Double

dMin = DateDiff("n", psTimeOne, psTimeTwo)

If dMin >= 60 Then
dHour = dHour + (dMin / 60)
TimeDiff = dHour
Else
dMin = dMin
TimeDiff = dMin * .01667
End If

End Function



Thanks wyler, now all I gotta do is grab the right 2 after calculation...;) Regards,
Anth:cool:ny
----------------------------------------
"You say [red]insanity[/red] like it's a BAD THING!"
 
You could also do it like that...
.3 / 60 would return .005, but you can always times that

Ok ok...I changed the code back to * 0.01 and just divided it by .6...works like a charm...=) Regards,
Anth:cool:ny
----------------------------------------
"You say [red]insanity[/red] like it's a BAD THING!"
 
Anthony, I think what you are asking is to be able to compute the difference between two time intervals and set the "granularity" to .5min. Seems very short, but regardless. Here is something that should work for you for any time granularity:
Use the DateDiff function for minutes to compute an elapsed time "ET". Then:
(ET \ 60) + ((Round((ET Mod 60) / Granularity))) * Granularity/ 60

The "\" is an integer divide operator. It will return just the interger part of the division operation. The (ET mod 60) returns just the remainder of the division operation. Divide that by the granularity and rounding it will give you number of intervals of time length = granularity. Multiply by decimal interval and you have a decimal amount of time rounded to the nearest granularity unit.

Am I making sense? Didn't want to be to verbose ... :)

Hope this help.

Dan Grogan
dan@siqual.com

"Absit prudentia nil rei publicae profitur."
Without common sense you ain't gonna have nothing.
 
Yea, I got it now, but took a slightly more verbose (in coding anyhow) way...I took wylers code, then did some alterations...THANKS WYLER! Regards,
Anth:cool:ny
----------------------------------------
"You say [red]insanity[/red] like it's a BAD THING!"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top