I'm trying to get local Moscow time in my .NET application.
Here is how far I went:
private static Dictionary<TimeZoneEnum, string> timeZoneDict = new Dictionary<TimeZoneEnum, string>()
{
{TimeZoneEnum.RussiaStandardTime, "Russian Standard Time"}
};
public static DateTime GetDateTimeNowForTimeZone(TimeZoneEnum timezone)
{
DateTime result = TimeZoneInfo.ConvertTime(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById(timeZoneDict[timezone]));
return result;
}
Method GetDateTimeNowForTimeZone gives me GMT+3 time but now in russia there is GMT+4 time. Is there anyu way to get robust solution for this issue in .net framework ?
You need to patch your system. On my system (Windows 7 with all updates installed) it gives the correct time:
DateTime.UtcNow.Dump();
TimeZoneInfo.ConvertTime(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("Russian Standard Time")).Dump();
output:
17/12/2013 09:11:08
17/12/2013 13:11:08
Microsoft has released an update a while ago to accomodate Russian DST law changes, you obviously don't have it.
*Dump() is an extension method provided by LINQPad. Equivalent of Console.WriteLine(<...>.ToString())
I think this issue will get resolved with a patch by Microsoft. They update the timezones periodically.
As far as I know for now all you can do is +1 the time.
Of course you could also go down the path of some web time service and request the time from it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With