C# Datetimes: Conversion for different time zones

前端 未结 6 753
[愿得一人]
[愿得一人] 2020-12-01 19:06

I have a bunch of date times that I keep track of for my app. They are all in UTC time. For part of my app I want to send an email with one of these times, but edited to be

相关标签:
6条回答
  • 2020-12-01 19:33
        var now = DateTime.Now; // Current date/time
        var utcNow = now.ToUniversalTime(); // Converted utc time
        var otherTimezone = TimeZoneInfo.FindSystemTimeZoneById("ANY OTHER VALID TIMEZONE"); // Get other timezone
        var newTime = TimeZoneInfo.ConvertTimeFromUtc(utcNow, otherTimezone); // New Timezone
    
    0 讨论(0)
  • 2020-12-01 19:47

    This should do the trick

    DateTime localTime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, TimeZoneInfo.Local);
    
    0 讨论(0)
  • 2020-12-01 19:48

    You can use javascript:

     var visitortime = new Date();
        vat time = visitortime.getTimezoneOffset()/60;
    

    After that you can save this value to any hidden control which is runat ="server".

    0 讨论(0)
  • 2020-12-01 19:50

    Use TimeZoneInfo.ConvertTimeFromUtc. The example listed there is pretty self explanatory.

    0 讨论(0)
  • 2020-12-01 19:53

    why not just

    TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, "AUS Eastern Standard Time");

    and check get all available timezones

    foreach (TimeZoneInfo tz in TimeZoneInfo.GetSystemTimeZones())
    {
        Console.WriteLine(tz.Id);
    }
    
    0 讨论(0)
  • 2020-12-01 19:56

    Use the TimeZoneInfo Class to convert a local time to a time in an alternative timezone:

    TimeZoneInfo est = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
    DateTime targetTime = TimeZoneInfo.ConvertTime(timeToConvert, est);
    
    0 讨论(0)
提交回复
热议问题