Forum Discussion

b2wise's avatar
b2wise
Helper III
3 years ago
Solved

Why is UTC datetime being converted to local datetime?

Hi all,   I am using DateTimeZone.UtcNow() to generate UTC time and I am using From.Number to convert it to a number so that I can subtract my local time and find the difference.   UTC datetime i...
  • AlexisOlson's avatar
    3 years ago

    The weird thing to me is that your seconds digits are different. I would expect them to match exactly. Maybe a caching thing? Does Refresh Preview make them match?

     

    The reason the number for local time and UTC should match is that they both represent the same thing, just in a different format. If you remove the time zone from UTC, then they'll be different.

    DateTimeZone.RemoveZone(DateTimeZone.UtcNow())

     

    To get the offset of your local time, you can use

    DateTimeZone.ZoneHours(DateTimeZone.LocalNow())

    If your local time zone also has a minutes offset, then you'll want to use DateTimeZone.ZoneMinutes or, alternatively, DateTimeZone.ToRecord to get all the components.