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

Subtracting Time 1

Status
Not open for further replies.

jass1220

Programmer
May 20, 2003
88
AE
what shall i do if i want to find out the time duration ...


txtFrom = 9:00

txtTo = 3:00

lblDuration = txtTo - txtFrom



thanks

 
For minutes use:

?(CDate("9:30")-CDate("9:00"))*1440

For Hours use:

?(CDate("9:30")-CDate("9:00"))* 24

I would use the above method especially in a loop where many recodes are being checked.

Otherwise, you can use DateDiff():

?DateDiff("n", "9:00", "9:30")
 
Code:
txtFrom = "9:00"
txtTo = "3:00"

lblDuration = DateDiff("h", CDate(txtFrom), CDate(txtTo)) & _
 ":" & DateDiff("n", CDate(txtFrom), CDate(txtTo))
Should to the trick.
 
Try this:
Code:
lblDuration = cDate(cDate(txtTo) - cDate(txtFrom))

or to keep formatting:
Code:
lblDuration = format((cDate(txtTo) - cDate(txtFrom)),"h:mm")


or have a look at DateDiff

hope this helps
Andreas
 

txtFrom = "9:00"
txtTo = "3:00"

lblduration= CDate(CDate(txtTo) - CDate(txtFrom))

but the result was in this formate 6:00:00 AM


i want it to be 6


i want to calculate the number of hours ...
 
Did you try CCLINT's solution?


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
I did mention this:

?(CDate("9:30")-CDate("9:00"))*1440

But you need to use Date Variables for the time or use 24 hour format, as 9:00 is strictly 9:00 AM.

Same as doing this:

CDate("3:00") which is always 3:00 AM.

The same will happen with
lblduration= CDate(CDate(txtTo) - CDate(txtFrom))

You can see this clearly on a machine set in 24 hour time format.

So, everything should work fine if you either use a 24 hour time format when working with strings, or use a date variable.

This is where it is a must to store time in a db field in a date/time field, and not in a text field, unless you have a 24 hour identifier such as 2 digit hours or using the AM/PM identifier.
Same holds true for user input.
Who knows what they mean when they input 3:00, unless they can select AM/PM or know that 3:00 is really 03:00.

jass1220, you need to be a little more observant.

Schweiger did give you a second choice so you only needed to use that, or a variant of that, such as using just "h".
For this, the Hour() or Minute function would be better though.
 
Thanks johnwm.

jass1220, let me say again: You will run into problems using:

txtFrom = "9:00"
txtTo = "3:00"

While this is 6 hours either way, changing txtTo = "4:00" is going to give you 5 hours and not the "expected" 7 hours!

For that you need to do this:
txtTo = "16:00"
or
txtTo = "4:00 PM"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top