'Converting UTC Datetime to local Datetime result in Error

Below is the line of code that is causing issue. I need to convert current UTC time to local time as per local time zone.

Note: Not using DateTime.Now because DateTime.Now provided cached value even after clearing the TimeZone cache when system time has been modified.

TimeZoneInfo.ConvertTime (DateTime.UtcNow, TimeZoneInfo.Local)
System.ArgumentException: Kind property of dateTime is Utc but the sourceTimeZone does not equal TimeZoneInfo.Utc

ERROR at System.TimeZoneInfo.ConvertTimeToUtc (System.DateTime dateTime, System.TimeZoneInfo sourceTimeZone, System.TimeZoneInfoOptions flags) <0x104615320 + 0x00164> in <34107616151548569c9b81c9b0f24a36#4e85f05b31daed62148693d6873ac2ea>:0 
2022-03-16 15:57:59.822677 +05:30   1   ERROR     at System.TimeZoneInfo.ConvertTimeToUtc (System.DateTime dateTime, System.TimeZoneInfo sourceTimeZone) <0x1046152e0 + 0x00027> in <34107616151548569c9b81c9b0f24a36#4e85f05b31daed62148693d6873ac2ea>:0 
2022-03-16 15:57:59.822677 +05:30   1   ERROR     at System.TimeZoneInfo.ConvertTime (System.DateTime dateTime, System.TimeZoneInfo sourceTimeZone, System.TimeZoneInfo destinationTimeZone) <0x104614ef0 + 0x000e3> in <34107616151548569c9b81c9b0f24a36#4e85f05b31daed62148693d6873ac2ea>:0 
2022-03-16 15:57:59.822677 +05:30   1   ERROR     at System.TimeZoneInfo.ConvertTime (System.DateTime dateTime, System.TimeZoneInfo destinationTimeZone) <0x104614e50 + 0x00077> in <34107616151548569c9b81c9b0f24a36#4e85f05b31daed62148693d6873ac2ea>:0

Sometimes it throws this error can somebody help me out



Solution 1:[1]

Contrary to what your question says, the exception message and stack trace make it clear that you're calling this overload:

public static DateTime ConvertTime(DateTime dateTime, TimeZoneInfo sourceTimeZone,
    TimeZoneInfo destinationTimeZone);

If we look at the Remarks for that overload:

DateTime.Kind value sourceTimeZone value Method behavior
DateTimeKind.Utc Equals TimeZoneInfo.Utc Converts dateTime to the destination time zone's time.
DateTimeKind.Utc Does not equal TimeZoneInfo.Utc Throws an ArgumentException.
DateTimeKind.Local Equals TimeZoneInfo.Local Converts dateTime to the destination time zone's time.
DateTimeKind.Local Does not equal TimeZoneInfo.Local Throws an ArgumentException.
DateTimeKind.Unspecified Any. Converts dateTime to the destination time zone's time.

The second line is the problematic one. You're passing a DateTime with a Kind of DateTimeKind.Utc (which is what you get from DateTime.UtcNow), but you're passing a sourceTimeZone which does not equal TimeZoneInfo.Utc, and so you're getting an ArgumentException.

Note how this overload is really only useful if your Kind is Unspecified. This makes sense: if the Kind is Utc or Local, TimeZoneInfo can work out what sourceTimeZone to use all by itself. If you insist on passing one, you need to make sure that you're passing the one which matches your dateTime. However if your Kind is Unspecified, then TimeZoneInfo doesn't know what time zone it's converting from, so you need to tell it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1