• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Coding Still

  • Home
  • About

Managing dates in .net

October 11, 2010 By _tasos Leave a Comment

Recently I faced an issue where I had to move an asp.net application with its data (Sql Server 2008) to a server that is located in a different country. I came across with two problems, both of them related to dates.

  • The first issue was that I exported some data from the Sql Server, using a very handy function ds.WriteToXml(). This function adds by default in all date fields a time offset which is the difference of your current timezone to the UTC.
  • The second issue was that I was saving all of my date information to UTC. The basic reason was that I had to make some calculations with my date data and I decided to save in UTC to avoid daylight saving issues. But when moving to a server in a different country, the dates that were saved in UTC were presented in the local time of the server (which was England) and not in the client’s local time (which was Greece).

Both issues were difficult to deal at the time of .NET 2.0. After that, the framework provided functions and structures that resolved such issues in a very straight forward way.

For the first issue, you need to know that every DataColumn field has a DateTimeMode property. This property can have specific values (Local, Unspecified, Unspecified Local, UTC). The default value is Local, so at the export of a dataset to an XML file, each date value had a +X offset (X=2 in my case, because the export occurred by a web server in Greece). When this XML file was used to load the data to a database in England, all date values were subtracted by X hours. I would expect that since the datetime field in Sql Server 2008 is timezone unaware that the default value would be Unspecified. In order to solve this, I wrote a simple for each loop in which I traversed all the columns of my DataTable and set for date columns the DateTimeMode to Unspecified. This simple tweak made my LoadXMLtoDb function to work like a charm. You can view about this case here.

For Each _Column As DataColumn In ds.Tables(0).Columns
    If (_Column.DataType Is System.Type.GetType("System.DateTime")) Then
        _Column.DateTimeMode = DataSetDateTime.Unspecified
    End If
Next

The second issue was the use of myDate.ToLocalTime(). I used this function to get the local time from my UTC values so that I will present them to the user. Moving the application to a web server in a different country, this function didn’t return the value I wanted. At first, I tried to set the thread’s culture to “el-GR” (following the instructions here) but it didn’t work for me. The solution I was looking for came by using the TimeZoneInfo namespace. Through that I could convert a UTC datetime to a specific timezone, and vice versa. The TimeZoneInfo namespace is simple to use:

Function ConvertToLocalTime(ByVal myUTCDate As DateTime, & _
                            ByVal tzID As String) As DateTime
    Dim myTzInfo As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(tzID)
 
    Return TimeZoneInfo.ConvertTimeFromUtc(myUTCDate, myTzInfo)
End Function
 
Function ConvertToUTC(ByVal myLocalDate As DateTime, & _
                      ByVal tzID As String) As DateTime
    Dim myTzInfo As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(tzID)
 
    Return TimeZoneInfo.ConvertTimeToUtc(myLocalDate, myTzInfo)
End Function

Also, in the TimeZone namespace you can get an array with all the available time zones. This information is built in the .NET framework and it is updated with Windows Update. So, in your project you can easily use this array instead of maintaining a table of your own.

TimeZonesDropDownList.DataSource = TimeZoneInfo.GetSystemTimeZones()
TimeZonesDropDownList.DataValueField = "id"
TimeZonesDropDownList.DataTextField = "DisplayName"
TimeZonesDropDownList.DataBind()

Issues regarding locality and dates are met more often by web developers where the web server, database server and client can be located in places with different locale and timezone. Personally, I deployed a web application that its web server was in England, the database server in USA and all the visitors where from Greece. The application had a simple log in and had to keep track of all visits and how long each visitor stayed in the home page to watch a live streaming. Not the deployment scenario I chose, but due to some problems I ended up with this setup. So it is important that such issues to be addressed in a universal way. In two lines my golden rules are:

  • Always store your date data in UTC. UTC doesn’t have daylight saving changes and it is universal. We can use it as base reference and convert the date to our desired timezone easily.
  • The use of the DateTime.ToLocalTime() is useful only in scenarios where you know that your web server is in a specific country. But when you have a product (e.g. a CMS) that it deploys in many servers, or you have an SaaS model (which could mean that you have visitors/customers from every corner of the world) you need to make all date conversion using timezone objects.

These two rules are simple to follow, but people might get easily confused with such issues. The .NET framework provides the required tools to handle such issues efficiently. The TimeZoneInfo namespace provides an array with all the time zones that are registered to the web server. This array gets updated from Windows Update. It is very easy to load the array to a DropDownList and let the user decide its timezone.

Filed Under: .NET Development, ASP.NET Tagged With: Date handling

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Categories

  • .NET Development
  • ASP.NET
  • Databases
  • Fun
  • IIS
  • JavaScript
  • Web Development

Tags

.NET Core Android ANTLR ASP.NET Ajax ASP.NET Core ASP.NET MVC ASP.NET Web Forms AWS Bouncy Castle Chartjs cli Client info detection Comic Continuous integration CSS Data backup Date handling Firebase Firefox addons Github HigLabo HTML5 Image manipulation jQuery JWT MySQL Nodejs Nuget OAuth Objectionjs OOP openssl Oracle ORM PHP Regular expressions SEO Social media SQL SQL Server UI/UX Url rewriting Videos Visual Studio Web design

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Secondary Sidebar

Archives

  • July 2020
  • March 2020
  • August 2019
  • December 2018
  • November 2018
  • February 2018
  • August 2016
  • June 2016
  • May 2016
  • February 2016
  • January 2016
  • August 2015
  • July 2015
  • October 2014
  • July 2014
  • November 2013
  • April 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • May 2012
  • February 2012
  • December 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010

Footer

Recent Posts

  • Anatomy of an Objection.js model
  • Check your RSA private and public keys
  • Round functions on the Nth digit
  • Send FCM Notifications in C#
  • Jwt Manager
  • Things around the web #5
  • Query JSON data as relational in MySQL
  • Create and sign JWT token with RS256 using the private key
  • Drop all database objects in Oracle
  • Create and deploy a Nuget package

Latest tweets

  • Geekiness Intensifies.. NASA used Three.js to render a real-time simulation of this week's NASA rover landing on M… https://t.co/orgkXnYj9O February 19, 2021 18:12
  • Things I Wished More Developers Knew About Databases https://t.co/h4gfq6NJgo #softwaredevelopment #databases May 3, 2020 12:52
  • How a Few Lines of Code Broke Lots of Packages https://t.co/p7ZSiLY5ca #javascript May 3, 2020 12:48
  • Can someone steal my IP address and use it as their own? https://t.co/HoQ7Z3BG69 January 24, 2020 13:27
  • Organizational complexity is the best predictor of bugs in a software module https://t.co/aUYn9hD4oa #softwaredevelopment January 13, 2020 08:24
  • http://twitter.com/codingstill

Misc Links

  • George Liatsos Blog
  • Plethora Themes
  • C# / VB Converter
  • Higlabo: .NET library for mail, DropBox, Twitter & more

Connect with me

  • GitHub
  • LinkedIn
  • RSS
  • Twitter
  • Stack Overflow

Copyright © 2021 · eleven40 Pro on Genesis Framework · WordPress · Log in