Convert UTC DateTime to another Time Zone

前端 未结 5 1752
渐次进展
渐次进展 2020-12-02 23:02

I have a UTC DateTime value coming from a database record. I also have a user-specified time zone (an instance of TimeZoneInfo). How do I convert that UTC DateTime to the us

相关标签:
5条回答
  • 2020-12-02 23:19

    The Antartica answer only works for the timezones matching UTC. I'm quite traumatized with this DateTimeOffset function and after hours of trial and error, I've managed to produce a practical conversion extension function that works with all timezones.

    static public class DateTimeFunctions
    {
        static public DateTimeOffset ConvertUtcTimeToTimeZone(this DateTime dateTime, string toTimeZoneDesc)
        {
            if (dateTime.Kind != DateTimeKind.Utc) throw new Exception("dateTime needs to have Kind property set to Utc");
            var toUtcOffset = TimeZoneInfo.FindSystemTimeZoneById(toTimeZoneDesc).GetUtcOffset(dateTime);
            var convertedTime = DateTime.SpecifyKind(dateTime.Add(toUtcOffset), DateTimeKind.Unspecified);
            return new DateTimeOffset(convertedTime, toUtcOffset);
        }
    }
    

    Example:

    var currentTimeInPacificTime = DateTime.UtcNow.ConvertUtcTimeToTimeZone("Pacific Standard Time");
    
    0 讨论(0)
  • 2020-12-02 23:24

    Have a look at the DateTimeOffset structure:

    // user-specified time zone
    TimeZoneInfo southPole =
        TimeZoneInfo.FindSystemTimeZoneById("Antarctica/South Pole Standard Time");
    
    // an UTC DateTime
    DateTime utcTime = new DateTime(2007, 07, 12, 06, 32, 00, DateTimeKind.Utc);
    
    // DateTime with offset
    DateTimeOffset dateAndOffset =
        new DateTimeOffset(utcTime, southPole.GetUtcOffset(utcTime));
    
    Console.WriteLine(dateAndOffset);
    

    For DST see the TimeZoneInfo.IsDaylightSavingTime method.

    bool isDst = southpole.IsDaylightSavingTime(DateTime.UtcNow);
    
    0 讨论(0)
  • 2020-12-02 23:24

    You can use a dedicated function within TimeZoneInfo if you want to convert a DateTimeOffset into another DateTimeOffset:

    DateTimeOffset newTime = TimeZoneInfo.ConvertTime(
        DateTimeOffset.UtcNow, 
        TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
    );
    
    0 讨论(0)
  • 2020-12-02 23:39
           //  TO get Currrent Time in current Time Zone of your System
    
            var dt = DateTime.Now;
    
            Console.WriteLine(dt);
    
            // Display Time Zone of your System
    
            Console.WriteLine(TimeZoneInfo.Local);
    
            // Convert Current Date Time to UTC Date Time
    
            var utc = TimeZoneInfo.ConvertTimeToUtc(dt, TimeZoneInfo.Local);
    
            Console.WriteLine(utc);
    
            // Convert UTC Time to Current Time Zone
    
            DateTime pacific = TimeZoneInfo.ConvertTimeFromUtc(utc, TimeZoneInfo.Local);
    
            Console.WriteLine(pacific);
    
            Console.ReadLine();
    
    0 讨论(0)
  • 2020-12-02 23:45

    The best way to do this is simply to use TimeZoneInfo.ConvertTimeFromUtc.

    // you said you had these already
    DateTime utc = new DateTime(2014, 6, 4, 12, 34, 0);
    TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
    
    // it's a simple one-liner
    DateTime pacific = TimeZoneInfo.ConvertTimeFromUtc(utc, tzi);
    

    The only catch is that the incoming DateTime value may not have the DateTimeKind.Local kind. It must either be Utc, or Unspecified.

    0 讨论(0)
提交回复
热议问题