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

calculating time

Status
Not open for further replies.

dfwcharles

Technical User
Apr 10, 2002
36
US
Here's there a simple way to calculate a specific time based on a time + a number of hours and minutes. I.E.

dim timein as date = "7:55 am"
dim hours as decimal = "8.05"
dim answer as date

answer = timein + hours

answer = 3:58 pm
I've tried Datediff but it doesnt give me what i need.

Any ideas?
 
Try this. It might not be 100 precise with casting to int, and then multiplying decimal part to 60 to get minutes, but it works fairly well. For example, with 1.99 hours, it gives you 2 hours later. With 1.98, it gives you 59 minutes later.

Code:
Dim MyDate As DateTime = #1/2/1900 7:55:00 AM#
Dim TimeToAdd As Double
TimeToAdd = 8.05
'Add Hours
MyDate = DateAdd(DateInterval.Hour, CInt(TimeToAdd), MyDate)
'Add Minutes
MyDate = DateAdd(DateInterval.Minute, (TimeToAdd - CInt(TimeToAdd)) * 60, MyDate)
MsgBox(MyDate)
 
Herein lies your problem

dim hours as decimal = "8.05"

8.05 hours is 8 hours and 3 minutes, not 8 hours and 5 minutes. 8 hours and 5 minutes is 8.0833333333 in decimal

Try

dim hours as string = "8.05"
MyDate = DateAdd(DateInterval.Hour, hours.Substring(0, hours.IndexOf(".")), MyDate)
MyDate = DateAdd(DateInterval.Minute, hours.Substring(hours.IndexOf(".") + 1), MyDate)

Craig
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top