Get timezone from DateTime

Does .Net DateTime contain time zone information where it was created?

I have a library that parses a DateTime from a format that has β€œ+ zz” at the end, and although it parses and sets the local time correctly, I need to get what the specific time zone was from a DateTime object.

Is this even possible? All I see is DateTime.Kind, which indicates whether the time is local or UTC.

+78
timezone c # datetime
Feb 23 '09 at 6:53
source share
5 answers

DateTime itself does not contain real-time information. He can know if it is UTC or local, but not what local really means.

DateTimeOffset is slightly better - it's mostly UTC time and offset. However, this is still not enough to determine the time zone, since many different time zones can have the same offset at any given time. It sounds like it might be enough for you, although all you need to work with when date / time parsing is an offset.

Support for time zones is much better than .NET 3.5 than it was, but I'd really like to see the standard "ZonedDateTime" or something like that - UTC time and actual time zone. It's easy to create your own, but it would be nice to see it in standard libraries.

EDIT: Almost four years later, I suggest using Noda Time , which has a richer set of date and time types. I am biased, though, as the main author of Noda Time :)

+111
Feb 23 '09 at 7:07
source share

No.

The developer is responsible for tracking the time zone information associated with the DateTime value through some external mechanism.

Quote from a great article here . A must read for every .Net developer.

So my advice is to write a small wrapper class that suits your needs.

+29
Feb 23 '09 at 7:03
source share

There is a TimeZone public domain library for .NET . Really helpful. It will meet your needs.

Solving a common time zone problem is harder than you think.

+5
Feb 24 '09 at 6:56
source share

You can use the TimeZoneInfo class

The TimeZone class recognizes the local time zone and can convert time between coordinated universal time (UTC) and local time. The TimeZoneInfo object can represent any time zone, and the methods of the TimeZoneInfo class can be used to convert time in one time zone to the corresponding time in any other time zone. Members of the TimeZoneInfo class support the following operations:

  • Getting a time zone that is already defined by the operating system.

  • Enumeration of time zones available in the system.

  • Convert time between different time zones.

  • Create a new time zone that is not yet defined by the operating system.

    Timezone serialization for later searches.

+2
Mar 13 2018-12-12T00:
source share

From the API ( http://msdn.microsoft.com/en-us/library/system.datetime_members(VS.71).aspx ), it seems that it cannot display the name of the time zone used.

+1
Feb 23 '09 at 7:05
source share



All Articles