Get timezone from DateTime

122,767

Solution 1

DateTime itself contains no real timezone information. It may know if it's UTC or local, but not what local really means.

DateTimeOffset is somewhat better - that's basically a UTC time and an offset. However, that's still not really enough to determine the timezone, as many different timezones can have the same offset at any one point in time. This sounds like it may be good enough for you though, as all you've got to work with when parsing the date/time is the offset.

The support for time zones as of .NET 3.5 is a lot better than it was, but I'd really like to see a standard "ZonedDateTime" or something like that - a UTC time and an actual time zone. It's easy to build your own, but it would be nice to see it in the standard libraries.

EDIT: Nearly four years later, I'd now suggest using Noda Time which has a rather richer set of date/time types. I'm biased though, as the main author of Noda Time :)

Solution 2

No.

A developer is responsible for keeping track of time-zone information associated with a DateTime value via some external mechanism.

A quote from an excellent article here. A must read for every .Net developer.

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

Solution 3

There is a public domain TimeZone library for .NET. Really useful. It will answer your needs.

Solving the general-case timezone problem is harder than you think.

Solution 4

You could use TimeZoneInfo class

The TimeZone class recognizes local time zone, and can convert times between Coordinated Universal Time (UTC) and local time. A TimeZoneInfo object can represent any time zone, and methods of the TimeZoneInfo class can be used to convert the time in one time zone to the corresponding time in any other time zone. The members of the TimeZoneInfo class support the following operations:

  1. Retrieving a time zone that is already defined by the operating system.

  2. Enumerating the time zones that are available on a system.

  3. Converting times between different time zones.

  4. Creating a new time zone that is not already defined by the operating system.

    Serializing a time zone for later retrieval.

Solution 5

From the API (http://msdn.microsoft.com/en-us/library/system.datetime_members(VS.71).aspx) it does not seem it can show the name of the time zone used.

Share:
122,767

Related videos on Youtube

dbkk
Author by

dbkk

Updated on July 08, 2022

Comments

  • dbkk
    dbkk almost 2 years

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

    I have a library parsing DateTime from a format that has "+zz" at the end, and while it parses correctly and adjusts a local time, I need to get what the specific time zone was from the DateTime object.

    Is this possible at all? All I can see is DateTime.Kind, which specifies if time is local or UTC.

    • yoyo
      yoyo almost 6 years
      See notes on DateTime.Parse documentation for handling of time zones and DateTimeStyles. But no, what you want isn't really possible.
  • Cheeso
    Cheeso about 15 years
    The PublicDomain projhect on CodePlex does this for you.
  • Jeff LaFay
    Jeff LaFay about 12 years
    A TimeZone enum in the BCL would be nice if the world time zones are static and don't change.
  • Jon Skeet
    Jon Skeet about 12 years
    @jlafay: However, they do change - more time zones were added to Windows just last year, for example.
  • Tomasz
    Tomasz about 12 years
    ...but, given a DateTime, there still isn't a way to use TimeZoneInfo to determine the DateTime's TimeZone, to my knowledge.
  • AnneTheAgile
    AnneTheAgile over 11 years
    This project is no longer maintained by the author; publicdomain.codeplex.com It seems like maybe this one might help, depending on the usage, one has to set it before using it; timezone.codeplex.com
  • Jon Skeet
    Jon Skeet over 11 years
    @AnneTheAgile: Personally I'd recommend using my own Noda Time library, of course :)
  • Konrad
    Konrad almost 5 years
    @RemiDespres-Smyth I just store TimeZoneInfo along with DateTime in 1 class.
  • Suncat2000
    Suncat2000 over 2 years
    The article can be summed up as: "DateTime in .NET has bugs and shortcomings. You deal with it."
  • ryanwebjackson
    ryanwebjackson about 2 years
    Converting the DateTime to Universal may not work, if the DateTime was created from a string in which the timezone was not known.