For most of us displaying the current date and time is summed up in the following statement: DateTime.Now;
No big deal, but what happens if the time that is displayed is of by a hour? Well most of us would check to see what the machines timezone is and adjust the timezone of the machine to the correct timezone.
But we are in Azure now, Windows Azure instances run on the GMT / UTC timezone, do we adjust the timezone here as well? We could by defining a startup task that adjust the timezone to your preferred one. And from a migration scenario this could be the cheapest and fastest method.
Doing some research on the DateTime usage turns out that there are some pretty good best practices around. In the case of Windows Azure its best to use UTC time in your application and convert it to the proper display value.
So instead of doing:
1: var currentDateTime = DateTime.Now;
Use the following:
1: var myTimeZone = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
2: var currentDateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, myTimeZone);
I leave the creation of the proper extension method up to you
One thought on “Working with DateTime.Now in Windows Azure apps”
Thanks a lot! Fixed a bug on my site 🙂