In this blog we will go trough best practices for handling DateTime in .NET applications:
1) We should always use a consistent date format across the application.
2) When accepting user input we need to ensure that user enters date time in correct date time format.
3) We should explicitly format dates when writing output to ensure consistency.
4) Calculations and comparisons of time stamps should be done from the same time-zone perspective.
5) Always use GMT or UTC format for saving an exact moment in time. If you choose to save time using a local time format,do save the local time offset from UTC also to ensure that saved time stamp can later be retrieved unambiguously.
6) We should always remember that time zone offsets can be fractional number of hours (for example, Indian Standard Time is UTC+05:30).
7) Most business logic is based on local time, instead of UTC/GMT. Therefore before we apply any business logic on them, the saved UTC timestamps should need to be converted to a local time zone.
8) We should remember that "time zone" such as IST with a "time zone offset" such as +05:30 are different things.
9) We should also never assume that time zones and offsets will never change. For example in 2007 the US extended Daylight Saving Time by several weeks starting in 2007.( http://www.timeanddate.com/news/time/us-daylight-saving-extended.html)
10) All OS and database servers should be kept in sync with each other and rest of the world.
11) In case we are using third-party libraries in the applications we should verify that they handle time zone data accurately.
12) If we need to perform DateTime calculations (add/subtract) on values representing time zones that practice daylight savings time it is a best practice to convert the local time value to universal time, perform the calculation, and convert back to achieve maximum accuracy.
0 Comment(s)