Problem with TimeZoneInfo.ConvertTime: missed the Daylight Saving switch.

Posted by SirMoreno on Stack Overflow See other posts from Stack Overflow or by SirMoreno
Published on 2010-03-27T20:36:17Z Indexed on 2010/03/27 20:43 UTC
Read the original article Hit count: 1199

Filed under:
|
|

My web app runs on .Net 3.5, all of the dates are saved on the DB in UTC time (not in user time).

When I want to display a date I convert it to user date (from UTC)

    //Get the current datetime of the user      exp: GMT TO ISRAEL +2
    public static DateTime GetUserDateTime(DateTime dateUTC)
    {
        string userTzId = "Israel Standard Time";
        TimeZoneInfo userTZ = TimeZoneInfo.FindSystemTimeZoneById(userTzId);

        dateUTC = DateTime.SpecifyKind(dateUTC, DateTimeKind.Utc);
        DateTime ret = TimeZoneInfo.ConvertTime(dateUTC, TimeZoneInfo.Utc, userTZ);
        return ret;
   }

Until now it worked fine but I have users from Israel (GMT +2), and Israel switched to Daylight saving time on 26/3/10 so now it's (GMT +3).

For some reason the TimeZoneInfo.ConvertTime don't know the Daylight saving time switch is on 26/3/10 so it still converts to GMT +2.

The strange thing is that on localhost it works fine, I set up a test page:

 DateTime userdate = GetUserDateTime(DateTime.UtcNow);
string str2 = "UserDateTime  = " + userdate.ToString("dd/MM/yy") + "  " + userdate.ToString("HH:mm");

On the Server (windows 2003 set to UTC time) it shows the wrong time (+2):

UserDateTime = 27/03/10 21:38

On localhost (windows XP set to Israel Time) it shows the correct time (+3):

UserDateTime = 27/03/10 22:38

How can I update the TimeZoneInfo that the Daylight saving time switch in Israel was on the 26/3/10?

Thanks.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about 3.5