Calculations on time

jhagerup

Member
Licensed User
Longtime User
Hi

Can anyone explain why this:

DateTime.TimeFormat="HH:mm:ss"
Log(DateTime.Time(DateTime.TimeParse("15:00:00")-DateTime.TimeParse("14:00:00")))

will give "02:00:00" and not "01:00:00" as expected ?

/jan
 
Hi Erel

Thanks, it works, but I don't understand why.

Subtracting Two "datetime" values (in same Timezone) should give the correct difference.
My original problem is/was that I want to calculate the difference between a timestamp (DateTime.now) and some datetime in the past.
The DateTime.Now returns in my timezone - or at least shows the correct values with DateTime.Time(DateTime.Now).
But a DateTime.TimeParse("sometime") returns a different time than "sometime" and not in my timezone.

The solution (based on your suggestion) was to save the Timestamp in my timezone, SetTimeZone(0), do the TimeParse and then SetTimeZone(MyTimezone).

/jan
 
Upvote 0
The problem is in the usage of DateTime.Time to convert ticks which do not represent an actual point of time to string.

You can use a custom method such as:
Code: Copy
Sub ConvertTicksToTimeString(t As Long) As String
    Dim days, hours, minutes, seconds As Int
    days = t / DateTime.TicksPerDay
    hours = (t Mod DateTime.TicksPerDay) / DateTime.TicksPerHour
    minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
    seconds = (t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
    Return days & " days, " & NumberFormat(hours, 2, 0) & " hours, " _
        & NumberFormat(minutes, 2, 0) & " minutes and " & NumberFormat(seconds, 2, 0) & " seconds"
End Sub

DateTime.Time(Ticks As Long) method, expects a value which represents the number of milliseconds passed since 1/1/1970, measured in UTC time zone (GMT).

If you pass 0 to this method you will get 1/1/1970 00:00 (GMT) converted to your time zone. It can even be more confusing if day light saving time is active or not.
 
Upvote 0
Thanks again.

The "little" hint that DateTime.Time expects UTC time was the clue.
Please update the wiki with very important information.

/jan
 
Upvote 0
how to sum hours?

I need to sum the hours day by day:
10:00:00
08:00:00
08:00:00
----------
26:00:00

how can i do this?

sorry my english
 
Upvote 0
Back
Top