Windows Azure Platform Developer Center > Microsoft Visual Studio 2010 Beta 2 Forums > Windows Azure > Convert datetime(stored in azure table) into local system datetime zone.
Ask a questionAsk a question
 

AnswerConvert datetime(stored in azure table) into local system datetime zone.

  • Monday, November 02, 2009 9:12 AMsabya singh Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi all,
    I want to convert the datetime stored in azure table into local system datetime in cloud application.can anyone suggest me the way to do so.
    any help would be appreciated.
    Thanks,
    Sabya.

Answers

  • Tuesday, November 03, 2009 6:33 AMYi-Lun LuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hello, this is very difficult to achieve in a pure server side solution. A standard http request does not include any information about the client's time zone.

    One (not very accurate) solution is to use the HttpWebRequest.UserHostAddress propery to get the underlying socket client's IP address. Then you can use this information to identify which country and thus which time zone the client is in. This approach is similar to detecting the client's country.

    Another solution is to go with a client side solution, such as Silverlight. You get the UTC time from a WCF service, and convert it to local time on the client side. Silverlight supports the DateTime.ToLocalTime method.

    Anyway, most server side web application (such as this forum) allows the user to select a preffered time zone.
    Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
  • Tuesday, November 03, 2009 11:58 AMAnton Staykov Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hello Sabya,
    As Yi-Lun Lou said - it is very hard to get local timezone from the client. And as displayed in my second post (and also explained by Yi-Lun) you could provide send users an option to save timezone in their profile (it would be one time save for the users and you will be reading it from the user's profile data). And in order to create a list of TimeZone to choose from, you can use the following code snippet to bind TimeZones to a DropDown list:
    //And get system's timezones into a drop down like this:
    TimeZoneDropdownList.DataTextField = "DisplayName";
    TimeZoneDropdownList.DataValueField = "Id"; 
    TimeZoneDropdownList.DataSource = TimeZoneInfo.GetSystemTimeZones();
    TimeZoneDropdownList.DataBind();
    
    

    Asume you have a "TimeZoneDropdownList" ASP.NET DropdownList control on your profiles page.

All Replies

  • Monday, November 02, 2009 9:33 AMAnton Staykov Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    Hello,
    When you save the DateTime, always save it as UTC , using the ToUniversalTime() method of DateTime instance.
    When you get it use ToLocalTime() method of DateTime instance.
    The question acutally is what is "Local System" for you? The client that browses the page, or the Web Application that serves the request.

    Hope this helps.
    Please mark this post as answer if it was helpful.
    • Proposed As Answer byAnton Staykov Monday, November 02, 2009 12:40 PM
    •  
  • Monday, November 02, 2009 9:53 AMsabya singh Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I tried with ToLocalTime() but it shows the date time as per the web application that serves the request.
    I want to convert it into datetime at client that browses the page.

    Regards,
    Sabya.

  • Monday, November 02, 2009 12:04 PMAnton Staykov Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed AnswerHas Code

    Hello,
    Checkout the comments after this: http://msdn.microsoft.com/en-us/library/bb384267.aspx
    Quoted:

    Store all dates as UTC:
    DateTime.UtcNow (or ToUniversalTime() )
    When displaying dates to users, use a specified timezone:
    //fetch the utc date from database
    DateTime someUsefulDate = GetSomeDateFromDatabase();
    //fetch desired timezone (see below)
    TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
    //adjust utc date to proper timezone date
    DateTime adjustedDate = TimeZoneInfo.ConvertTimeFromUtc(someUsefulDate ,zone);
    //For web applications, you can have the user pick their timezone. You can save this choice in a profile or session etc..
    //And get system's timezones into a drop down like this:
    TimeZoneDropdownList.DataTextField = "DisplayName";
    TimeZoneDropdownList.DataValueField = "Id"; 
    TimeZoneDropdownList.DataSource = TimeZoneInfo.GetSystemTimeZones();
    TimeZoneDropdownList.DataBind();<br/>
    
    And actually giving your users option to choose which timezone they are is a good option, becasue you can't rely on specific Culture. For example: "en-US - English, American" contains 6 timezones.

    Hope this helps.
    • Proposed As Answer byAnton Staykov Monday, November 02, 2009 12:40 PM
    •  
  • Tuesday, November 03, 2009 6:33 AMYi-Lun LuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hello, this is very difficult to achieve in a pure server side solution. A standard http request does not include any information about the client's time zone.

    One (not very accurate) solution is to use the HttpWebRequest.UserHostAddress propery to get the underlying socket client's IP address. Then you can use this information to identify which country and thus which time zone the client is in. This approach is similar to detecting the client's country.

    Another solution is to go with a client side solution, such as Silverlight. You get the UTC time from a WCF service, and convert it to local time on the client side. Silverlight supports the DateTime.ToLocalTime method.

    Anyway, most server side web application (such as this forum) allows the user to select a preffered time zone.
    Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
  • Tuesday, November 03, 2009 11:36 AMsabya singh Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Anton,

    Thanks a lot for your valuable suggestion.
    But my requirement is to convert the datetime(stored in azure table in UTC format) to client side datetime that browses the page.
    as its hardcoded the key used for timeZone in the suggested piece of code
    TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");

    Not getting how to get TimeZoneInfo?

    Could you please suggest me how to get TimezoneInfo from client that browses the page.

    Regards,
    Sabya.
  • Tuesday, November 03, 2009 11:58 AMAnton Staykov Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hello Sabya,
    As Yi-Lun Lou said - it is very hard to get local timezone from the client. And as displayed in my second post (and also explained by Yi-Lun) you could provide send users an option to save timezone in their profile (it would be one time save for the users and you will be reading it from the user's profile data). And in order to create a list of TimeZone to choose from, you can use the following code snippet to bind TimeZones to a DropDown list:
    //And get system's timezones into a drop down like this:
    TimeZoneDropdownList.DataTextField = "DisplayName";
    TimeZoneDropdownList.DataValueField = "Id"; 
    TimeZoneDropdownList.DataSource = TimeZoneInfo.GetSystemTimeZones();
    TimeZoneDropdownList.DataBind();
    
    

    Asume you have a "TimeZoneDropdownList" ASP.NET DropdownList control on your profiles page.