Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • .Net best practices for timezone

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 232
    Comment on it

    Hello Readers,

    When the system/web application is not used just in a single location, the developer will have to deal with many times zones in the system/application. There are few rules that every developer should keep in mind while dealing with multiple times zones.

    • The calculations and comparisons of DateTime should be done only when both instances are represented from the same time zone. For example: if both instances are from the same time-zone like IST (GMT + 5:30), then calculations/comparisons can be performed to get the correct result. If one instance from IST (GMT + 5:30) and another instance from CST(GMT-6:00) then comparisons or calculations will result you the wrong result.
    • To store a DateTime value, use the local time offset and time-zone information associated with a DateTime value.
    • You should always convert DateTime values to universal time representations before performing date arithmetic calculations.
    • While doing calculations on an instance of DateTime values, it does not modify the values of the instance. So, localDateTime.ToLocalTime() can be used to not modify the values of the DateTime.

    Best practices are:

    • Storing DateTime values as UTC to maintaining a consistent baseline date value.
    • DateTime.UtcNow is used to get UTC format from system generated values.
    • .ToUniversalTime() is used to get UTC format from user inputs.
    • In web-application, you also need to be able to convert user local dates to and from specific time zones which you can do with TimeZoneInfo.ConvertTime() and THEN convert that value to UTC.
    • When retrieving dates, all the dates need to be converted from UTC dates to local dates according to the user’s time zone for this you have to use the TimeZoneInfo class. There are two methods that are used from the TimeZoneInfo class i.e. TimeZoneInfo.FindSystemTimeZoneById() and TimeZoneInfo.ConvertTimeFromUtc().
    • TimeZoneInfo.ConvertTime() converts between two timezones and TimeZoneInfo.ConvertTimeToUtc().

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: