Question:
How can I calculate the difference between two time with Visual Basic?
Arash
2009-05-12 04:53:15 UTC
I want to calculate the difference between two time for my company services in this method:
A service is scheduled to start at 15:00 and it will start at 15:00. It's On-Time.
Another service is scheduled to start at 15:00 but it will start at 15:30. The difference between these two time are 00:30 and it's Delayed.
It's OK with using Timespan in VS until I used this example:
A Service is scheduled to start at 23:50 but it will start at 00:30. We know the difference is 40 Minutes (00:40) but VS gives me 23:20!!!!
It will calculate the Hours and Minutes separately and gives you the 23 Hours difference!
And please note that the date is not important if you want to say that check if the time was in Today or if the time is Tomorrow. The Date is not important.

What should I do?
Thanks.
Three answers:
?
2009-05-12 17:35:48 UTC
Timespan will work when you include the correct dates in the time.

Your example uses times before and after midnight when the calendar changes yet you have excluded the correct date from both times. Dates are based upon the number of days since 1/1/1900 so all date math is based on Double precession values.



You either have to include the actual dates when these times ocurred OR account for the extra calendar day in the time after midnight.







Place the following code in a Button click event then use DEBUG mode to step through it. (or just read the comments)



----------------------------------------------------------------------------------------------

Dim date1, date2 As Date

Dim mySpan As TimeSpan



'Windows dates are based upon whole number of days since 1/1/1900

'fractions of a day represent the hours,minutes and seconds.

' So 0.5 is Noon and 0.9999 is moments before midnight



date1 = "23:50" '= 0.993055555555556

date2 = "00:30" '= 0.0208333333333333



Debug.Print(date1.ToOADate())

'Take difference between dates and store in timespan

mySpan = date2 - date1 '= -1400 minutes



date1 = "5/11/2009 23:50" '= 39944.9930555556 (39944 days since 1/1/1900

'while .9930555556 is 11:50PM or 23:50)

'

date2 = "5/12/2009 00:30" '= 39945.0208333333



mySpan = date2 - date1 '= 40 minutes

'39945.0208333333 - 39944.9930555556 = 0.0277777777 days

'0.0277777777 * 24 hours = 0.6666666648 hours 24hrs/day

'0.6666666648 * 60 minutes = 39.999999888 minutes 60min/hr

'



Debug.Print(mySpan.TotalMinutes) 'displays teh current 40 minutes



----------------------------------------------------------------------------------------------------
anonymous
2009-05-12 05:18:57 UTC
DateDiff(). Use n (minutes) or even s (seconds) for the interval. (The difference between 00:40 and 23:50 on the same date is 23 hours and 20 minutes - you HAVE TO use the date. It's not VS, it's how numbers work. (times without dates are assumed to be on the same day by human thought.)



So the date *IS* important.
?
2016-10-05 11:10:13 UTC
have not got one obtainable on the 2nd (it somewhat is at artwork and servers are recent technique improve). think of of the function as a black container whre you grant the mandatory enter/parameter i.e. a persevering with or variable that conforms to the form of documents required. examine extra on the function itself.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...