Creating a DateTime in a specific Time Zone in c#

243,353

Solution 1

Jon's answer talks about TimeZone, but I'd suggest using TimeZoneInfo instead.

Personally I like keeping things in UTC where possible (at least for the past; storing UTC for the future has potential issues), so I'd suggest a structure like this:

public struct DateTimeWithZone
{
    private readonly DateTime utcDateTime;
    private readonly TimeZoneInfo timeZone;

    public DateTimeWithZone(DateTime dateTime, TimeZoneInfo timeZone)
    {
        var dateTimeUnspec = DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified);
        utcDateTime = TimeZoneInfo.ConvertTimeToUtc(dateTimeUnspec, timeZone); 
        this.timeZone = timeZone;
    }

    public DateTime UniversalTime { get { return utcDateTime; } }

    public TimeZoneInfo TimeZone { get { return timeZone; } }

    public DateTime LocalTime
    { 
        get 
        { 
            return TimeZoneInfo.ConvertTime(utcDateTime, timeZone); 
        }
    }        
}

You may wish to change the "TimeZone" names to "TimeZoneInfo" to make things clearer - I prefer the briefer names myself.

Solution 2

The DateTimeOffset structure was created for exactly this type of use.

See: http://msdn.microsoft.com/en-us/library/system.datetimeoffset.aspx

Here's an example of creating a DateTimeOffset object with a specific time zone:

DateTimeOffset do1 = new DateTimeOffset(2008, 8, 22, 1, 0, 0, new TimeSpan(-5, 0, 0));

Solution 3

The other answers here are useful but they don't cover how to access Pacific specifically - here you go:

public static DateTime GmtToPacific(DateTime dateTime)
{
    return TimeZoneInfo.ConvertTimeFromUtc(dateTime,
        TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"));
}

Oddly enough, although "Pacific Standard Time" normally means something different from "Pacific Daylight Time," in this case it refers to Pacific time in general. In fact, if you use FindSystemTimeZoneById to fetch it, one of the properties available is a bool telling you whether that timezone is currently in daylight savings or not.

You can see more generalized examples of this in a library I ended up throwing together to deal with DateTimes I need in different TimeZones based on where the user is asking from, etc:

https://github.com/b9chris/TimeZoneInfoLib.Net

This won't work outside of Windows (for example Mono on Linux) since the list of times comes from the Windows Registry: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\

Underneath that you'll find keys (folder icons in Registry Editor); the names of those keys are what you pass to FindSystemTimeZoneById. On Linux you have to use a separate Linux-standard set of timezone definitions, which I've not adequately explored.

Solution 4

I altered Jon Skeet answer a bit for the web with extension method. It also works on azure like a charm.

public static class DateTimeWithZone
{

private static readonly TimeZoneInfo timeZone;

static DateTimeWithZone()
{
//I added web.config <add key="CurrentTimeZoneId" value="Central Europe Standard Time" />
//You can add value directly into function.
    timeZone = TimeZoneInfo.FindSystemTimeZoneById(ConfigurationManager.AppSettings["CurrentTimeZoneId"]);
}


public static DateTime LocalTime(this DateTime t)
{
     return TimeZoneInfo.ConvertTime(t, timeZone);   
}
}

Solution 5

I like Jon Skeet's answer, but would like to add one thing. I'm not sure if Jon was expecting the ctor to always be passed in the Local timezone. But I want to use it for cases where it's something other then local.

I'm reading values from a database, and I know what timezone that database is in. So in the ctor, I'll pass in the timezone of the database. But then I would like the value in local time. Jon's LocalTime does not return the original date converted into a local timezone date. It returns the date converted into the original timezone (whatever you had passed into the ctor).

I think these property names clear it up...

public DateTime TimeInOriginalZone { get { return TimeZoneInfo.ConvertTime(utcDateTime, timeZone); } }
public DateTime TimeInLocalZone    { get { return TimeZoneInfo.ConvertTime(utcDateTime, TimeZoneInfo.Local); } }
public DateTime TimeInSpecificZone(TimeZoneInfo tz)
{
    return TimeZoneInfo.ConvertTime(utcDateTime, tz);
}
Share:
243,353

Related videos on Youtube

Jack Hughes
Author by

Jack Hughes

I have been an enthusiastic programmer since 1988 and have been a professional software developer since 1992.

Updated on January 08, 2022

Comments

  • Jack Hughes
    Jack Hughes over 2 years

    I'm trying to create a unit test to test the case for when the timezone changes on a machine because it has been incorrectly set and then corrected.

    In the test I need to be able to create DateTime objects in a none local time zone to ensure that people running the test can do so successfully irrespective of where they are located.

    From what I can see from the DateTime constructor I can set the TimeZone to be either the local timezone, the UTC timezone or not specified.

    How do I create a DateTime with a specific timezone like PST?

    • Oded
      Oded almost 14 years
    • Suncat2000
      Suncat2000 over 2 years
      Your description of the DateTime constructor specifies a DateTimeKind, not a Time Zone. DateTimeKind has extremely limited usefulness.
  • teashark
    teashark almost 15 years
    will this kind of struct map to a IQueryable interface on LinqToSql? coz any time i've tried to use another object instead of DateTime or DateTime? on my LinqToSql mapping, it fails when i query it.. u prob know the exception: "..can not convert to SQL.."
  • Jon Skeet
    Jon Skeet almost 15 years
    I don't know of any equivalent SQL Server construct, I'm afraid. I would suggest having the time zone name as one column, and the UTC value in another column. Fetch them separately and then you can create instances fairly easily.
  • IDisposable
    IDisposable over 14 years
    Not sure about the expected use of the constructor that takes a DateTime and TimeZoneInfo, but given that you're calling the dateTime.ToUniversalTime() method, I suspect you are guessing it to "maybe" be in local time. In that case, I think you should really be using the passed-in TimeZoneInfo to convert it to UTC since they're telling you it is supposed to be in that timezone.
  • Redth
    Redth about 14 years
    Thanks, this is a good way to accomplish it. After you get your DateTimeOffset object within the right timezone, you can use the .UtcDateTime property to get a UTC time for the one you created. If you store your dates in UTC, then converting them to local time for each user is no big deal :)
  • Ben Foster
    Ben Foster almost 13 years
    +1 - just noticed a bug in the LocalTime property - the variable should be "utcDateTime" not "utcTime".
  • amiry jd
    amiry jd over 12 years
    @JonSkeet thanks to answer. a nice idea is presented. just a little note: you set the private fields as read-only, and give them value from .ctor, but the type is a struct and structs have a default .ctor always. regards.
  • Jon Skeet
    Jon Skeet over 12 years
    @Javad_Amiry: Yes, it means that the "default value" of the struct is unfortunately unusable. You could potentially make having a null time zone equivalent to (say) UTC.
  • Chris Moschini
    Chris Moschini about 12 years
    I've created a library that uses a similar object, and helps map states/countries to timezones, convert between them, etc. github.com/b9chris/TimeZoneInfoLib.Net - demo: timezoneinfolib.brass9.com
  • Jon Skeet
    Jon Skeet about 12 years
    @ChrisMoschini: I'd actually try to avoid using the abbreviations if possible. They're ambiguous. I'd also argue that the name "UtcTimeZone" really isn't what your class represents. I'd expect that to be some subclass of an abstract time zone type of some description, representing the UTC zone.
  • Chris Moschini
    Chris Moschini almost 11 years
    Old comment, but I ultimately kept the abbreviations in my library. I find them very useful in both code and in simple storage in the db (just store a UTC and the short timezone name - DateTime and String, addressing @cottsak's issue). I find them very readable rather than confusing - but that's why there's more than one library for many tasks - authors unsurprisingly prefer their own libraries.
  • Jon Skeet
    Jon Skeet almost 11 years
    @ChrisMoschini: How do you deal with the fact that the abbreviations aren't unique? They don't really identify a time zone. If your code is only used in the US it may be okay, but if you ever want to expand elsewhere, I think it's wise to have unique IDs.
  • Chris Moschini
    Chris Moschini almost 11 years
    @JonSkeet You just use unique names for each time zone - not complicated. For example both the US and Australia have eastern standard time, so you use EST for US, and AUEST for Aus. You can still display to the user in the standard, non-unique abbreviations if you like so there's no harm there, and as I said it makes data and code much easier to read than some crazy numeric ID or other meaningless-but-unique identifier.
  • Jon Skeet
    Jon Skeet almost 11 years
    @ChrisMoschini: At that point you're just inventing your own ID scheme though - a scheme which no-one else in the world uses. I'll stick with the industry-standard zoneinfo, thanks. (It's hard to see how "Europe/London" is meaningless, for example.)
  • Chris Moschini
    Chris Moschini almost 11 years
    @JonSkeet Yes like I said, it's unsurprising authors prefer their own libraries. My approach makes it easy to store in the db, and easy to verify quickly - I prefer it, no surprises.
  • Jon Skeet
    Jon Skeet almost 11 years
    @ChrisMoschini: There's a difference between "prefer their own library" and "prefer an industry-wide standard" surely. Given the choice between two libraries I hadn't been involved in, I'd rather go with one which used standardize IDs. This isn't a case of NIH-syndrome.
  • Chris Moschini
    Chris Moschini almost 11 years
    @JonSkeet There is in fact a standard for these abbreviations: timeanddate.com/library/abbreviations/timezones (and my apologies the Aus EST there is AEST, not AUEST). So by your measure, there is no difference. What is different is - the library I wrote best solves the problem I was looking to solve, and I unsurprisingly prefer it. You prefer yours. I don't think there's a lot more to say. We can both have our libraries out there, whoever they help they help, we're not making any money off them so there's not much point in competing. I hope people find them both useful.
  • Jon Skeet
    Jon Skeet almost 11 years
    @ChrisMoschini: Yes, there are abbreviations - which are ambiguous, so shouldn't be used as identifiers. (CET for example.) In order to get away from ambiguity, you've had to get away from the standard. There's a significant difference between someone using your library and someone using Noda Time: anyone using Noda Time can interoperate with any other system using TZDB. There are an awful lot of those. How many other systems are using your unambiguous abbreviation set? (There are other advantages in terms of identifying a time zone rather than "half" a time zone (e.g. BST) as well.)
  • Chris Moschini
    Chris Moschini almost 11 years
    You might be glancing too quickly - CET means the same thing - it's always UTC+1. There's no ambiguity.
  • Jon Skeet
    Jon Skeet almost 11 years
    @ChrisMoschini: Different example then: CST. Is that UTC-5 or UTC-6? How about IST - is that Israel, India or Ireland in your database? (And even if you know the offset right now, different countries observing the same abbreviation may change at different times. So there's still ambiguity about which actual time zone it means. Time zone != offset.) Going back to your case: you claim that using abbreviations best solved your problem. How would using industry standard time zone IDs have been worse?
  • Chris Moschini
    Chris Moschini almost 11 years
    I'm not trying to code for region, just DST pattern. And for the problem I'm trying to solve we're using a small number of unambiguous, rough time zones (if you look at the code I note I don't even bother with the complexities of the state of Indiana). It's simpler to check for, and easier to store. This discussion is running too long for an SO answer so I'm going to stop responding here. If you dislike my library that's fine, but I'm not going to make it work just like yours. It solves the problem I set out to address. If it solves someone else's... cool.
  • Jon Skeet
    Jon Skeet almost 11 years
    @ChrisMoschini: Well I'll continue to recommend using the industry-standard, unambiguous zoneinfo IDs rather than the ambiguous abbreviations. This isn't a matter of whose library is preferred - the authorship of the library really isn't an issue. If someone wishes to use another library with a good choice of identifier, that's fine. The choice of identifier for a time zone is an important one though, and I think it's very important that readers are aware that the abbreviations are ambiguous, as I've shown with the IST example.
  • Brent
    Brent over 8 years
    Additionally there is ConvertTimeBySystemTimeZoneId() ex: TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, "Central Standard Time")
  • crokusek
    crokusek over 8 years
    I don't think this handles Daylight Savings Time correctly since some TimeZones honor it while others don't. Also "on the day" DST begins/ends, portions of that day would be off.
  • Triynko
    Triynko about 8 years
    Lesson. DST is a rule of a particular time zone. DateTimeOffset is not not not not not associated with any time zone. Do not confuse a UTC offset value, such as -5, with a time zone. It's not a time zone, it's an offset. The same offset is often shared by many time zones, so it's an ambiguous way of referring to a time zone. Since DateTimeOffset is associated with an offset, not a timezone, it cannot possibly apply DST rules. So 3am will be 3am on every single day of the year, without exception in a DateTimeOffset structure (e.g. in it's Hours and TimeOfDay properties).
  • Triynko
    Triynko about 8 years
    Where you may get confused is if you look at the LocalDateTime property of the DateTimeOffset. That property is NOT a DateTimeOffset, it's a DateTime instance whose kind is DateTimeKind.Local. That instance IS associated with a time zone... whatever the local system timezone is. That property WILL reflect daylight savings.
  • Triynko
    Triynko about 8 years
    So, the real problem with DateTimeOffset is that it doesn't include enough information. It's includes an offset, not a time zone. The offset is ambiguous with multiple time zones.
  • Triynko
    Triynko about 8 years
    If it did have a specific time zone associated with it, not only would it have the offset, but it would also have all the rules associated with that time zone, including when daylight savings starts and ends (if it even supports it), the magnitude of the offset (usually just 1 hour), whether it begins on a fixed date or a flexible one like 1st Sunday of March, and even whether a particular point in time is invalid, ambiguous, or is within the DST range of the year. You need TimeZoneInfo for all the information. And now you know why Jon Skeet's structure above is awesome.
  • Machado
    Machado over 4 years
    Sorry, but it's not available on Asp .NET Core 2.2 here, VS2017 is suggesting me to install an Outlook Nuget package.
  • Gopi
    Gopi over 4 years
    @JonSkeet I've set the TimeZone as India Standard Time and when I try LocalTime.ToString(), instead of 13/12/2019 4:00:00 PM, I am getting 12/13/2019 4:00:00 PM. I need to set this conversion based on TimeZone.
  • Jon Skeet
    Jon Skeet over 4 years
    @Gopi: Rather than adding a comment to a post that from over 10 years ago, please ask a new question with more details - but be aware that a format and a time zone are entirely different things.
  • Gopi
    Gopi over 4 years
    @JonSkeet Thanks. Agreed, Format and Timezone are different things but my intention is to format the date based on timezone. I've created a separate question. stackoverflow.com/questions/59279009/…
  • AZ_
    AZ_ about 4 years
    example => TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"))
  • yu yang Jian
    yu yang Jian about 4 years
    In windows TimeZone Id List also can see this answer: stackoverflow.com/a/24460750/4573839
  • Suncat2000
    Suncat2000 over 2 years
    FYI: The TimeZone class was deprecated long ago. It was too limited, just like DateTimeKind was too limited. TimeZoneInfo was a significant improvement but failed to identify when to apply - and when not to apply - daylight saving time adjustments.
  • Suncat2000
    Suncat2000 over 2 years
    DateTimeOffset does not specify a time zone. @Tryinko explains it well in his comments.
  • JSON
    JSON about 2 years
    This won't take into account daylight savings time
  • InteXX
    InteXX about 2 years
    Where are you getting TimeZones.Paris.Id?