7

I want to show Date & Time of an event which will be managed according to time zone of user. To check time zone I change my system time Zone to another time zone but my code is still getting Local time Zone. Here's My code

I am using Cassendra Database and C# .NET MVC

DateTime startTimeFormate = x.Startdate;
DateTime endTimeFormate = x.Enddate;
TimeZone zone = TimeZone.CurrentTimeZone;
DateTime startTime = zone.ToLocalTime(startTimeFormate);
DateTime endTime = zone.ToLocalTime(endTimeFormate);
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
Champ Prateek
  • 81
  • 1
  • 1
  • 8
  • Possible duplicate of [How to get current user timezone in c#](https://stackoverflow.com/questions/8194016/how-to-get-current-user-timezone-in-c-sharp) – ArunPratap Jan 28 '19 at 07:22
  • If you care about timezones use `DateTimeOffset` at least, not `DateTime`. As for your issue, what's the `DateTimeKind` value for `x.StartDate` and `x.EndDate`? Local, UTC or Unspecified? `ToLocalTime` only makes sense when you want to convert UTC to local. In the other cases, the only logical result is to return the same datetime value with a `DateTimeKind` of `Local`. – Panagiotis Kanavos Jan 28 '19 at 07:48
  • If the values come from the database, they are probably `Unspecified` as the database provider has no idea what kind of date they represent, or which offset to use. – Panagiotis Kanavos Jan 28 '19 at 07:52
  • Finally, to convert `DateTime` values from one timezone to another, use [TimezoneInfo.ConvertTime](https://learn.microsoft.com/en-us/dotnet/api/system.timezoneinfo.converttime?view=netframework-4.7.2#System_TimeZoneInfo_ConvertTime_System_DateTime_System_TimeZoneInfo_) – Panagiotis Kanavos Jan 28 '19 at 07:54

4 Answers4

11

To convert the UTC DateTime to your Local DateTime, you have to use TimeZoneInfo as follows:

DateTime startTimeFormate = x.Startdate; // This  is utc date time
TimeZoneInfo systemTimeZone = TimeZoneInfo.Local;
DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(startTimeFormate, systemTimeZone);

Moreover if you want to convert UTC DateTime to user specific Local DateTime then do as follows:

string userTimeZoneId = "New Zealand Standard Time";
TimeZoneInfo nzTimeZone = TimeZoneInfo.FindSystemTimeZoneById(userTimeZoneId);
DateTime userLocalDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, userTimeZoneId);

Note: TimeZone in .NET is obsolete now and it has been deprecated. Instead use TimeZoneInfo.

TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
2

TimeZone.CurrentTimeZone, TimeZoneInfo.Local and ToLocalTime use the local time zone of the server, not the end-user.

Instead, first see how to reliably get the end-users's time zone in your .NET code.

Then, assuming you now have a TimeZoneInfo object, simply use the TimeZoneInfo.ConvertTimeFromUtc method.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
1

These are DateTime helpers i use and that cover all of the cases i needed so far.

public static class DateTimeHelpers
  {
    public static DateTime ConvertToUTC(DateTime dateTimeToConvert, string sourceZoneIdentifier)
    {
      TimeZoneInfo sourceTZ = TimeZoneInfo.FindSystemTimeZoneById(sourceZoneIdentifier);
      TimeZoneInfo destinationTZ = TimeZoneInfo.FindSystemTimeZoneById("UTC");

      return TimeZoneInfo.ConvertTime(dateTimeToConvert, sourceTZ, destinationTZ);
    }

    public static DateTime ConvertToTimezone(DateTime utcDateTime, string destinationZoneIdentifier)
    {
      TimeZoneInfo sourceTZ = TimeZoneInfo.FindSystemTimeZoneById("UTC");
      TimeZoneInfo destinazionTZ = TimeZoneInfo.FindSystemTimeZoneById(destinationZoneIdentifier);

      return DateTime.SpecifyKind(TimeZoneInfo.ConvertTime(utcDateTime, sourceTZ, destinazionTZ), DateTimeKind.Local);
    }

    public static DateTime GetCurrentDateTimeInZone(string destinationZoneIdentifier)
    {
      TimeZoneInfo sourceTZ = TimeZoneInfo.FindSystemTimeZoneById("UTC");
      TimeZoneInfo destinazionTZ = TimeZoneInfo.FindSystemTimeZoneById(destinationZoneIdentifier);

      return DateTime.SpecifyKind(TimeZoneInfo.ConvertTime(DateTime.UtcNow, sourceTZ, destinazionTZ), DateTimeKind.Local);
    }
  }
dee zg
  • 13,793
  • 10
  • 42
  • 82
0

According to MSDN documentation of the TimeZone.CurrentTimeZone property, the local time zone is cached after the first call to TimeZone.CurrentTimeZone. In practice, this means your code should run fine as long as dynamic updates of time zone mid-run should not be supported. In order to see the changes immediately, before calling TimeZone.CurrentTimeZone you should call

TimeZoneInfo.ClearCachedData();

This is documented in the MSDN article as follows:

Notes to Callers

Local time zone data is cached after CurrentTimeZone is first used to retrieve time zone information. If the system's local time zone subsequently changes, the CurrentTimeZone property does not reflect this change. If you need to handle time zone changes while your application is running, use the TimeZoneInfo class and call its ClearCachedData() method.

Efi Z
  • 182
  • 12