Globally Convert Utc Datetimes to User Specified Local Datetimes

How to identify the timezone of the users

A commonly used scheme to achieve this would be to store all dates in UTC and then convert to local time zone in client code (javacsript/jquery/whatever other javascript framework you want to use).

A simple controller action demonstrating saving a utc date:

[HttpPost]
public ActionResult SaveDate()
{
try
{
_dateRepository.Save(DateTime.UtcNow);

return Index(date);
}
catch (Exception ex)
{
_log.Error(ex.Message, ex);
}
}

and then on load of the page where you want to display the data, you can have a script to convert the time to a local timezone as detailed here:

var localDateTime = utcDateTime.ToString();

You would then display the javascript-converted data to the user. This allows you to save all dates in a uniform format and not have to worry about keeping track of timezone information.

Convert UTC DateTime to another Time Zone

Have a look at the DateTimeOffset structure:

// user-specified time zone
TimeZoneInfo southPole =
TimeZoneInfo.FindSystemTimeZoneById("Antarctica/South Pole Standard Time");

// an UTC DateTime
DateTime utcTime = new DateTime(2007, 07, 12, 06, 32, 00, DateTimeKind.Utc);

// DateTime with offset
DateTimeOffset dateAndOffset =
new DateTimeOffset(utcTime, southPole.GetUtcOffset(utcTime));

Console.WriteLine(dateAndOffset);

For DST see the TimeZoneInfo.IsDaylightSavingTime method.

bool isDst = southpole.IsDaylightSavingTime(DateTime.UtcNow);

Convert UTC date time to local date time

Append 'UTC' to the string before converting it to a date in javascript:

var date = new Date('6/29/2011 4:52:48 PM UTC');
date.toString() // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)"

Convert utc to local datetime

Microsoft Azure is set to have UTC time be the local time no matter where the data center is located. If you want to convert to a time that is local to a user's browser you will need to convert it programatically.

Since Azure is a global service, there really is no local time per se, and to be consistent across all their global data centers, it makes sense to use a standard time.

More information at.

http://michaelcollier.wordpress.com/2010/05/22/hey-azure-what-time-is-it/

Should I use DateTime UTC for an international application

In general, you should prefer to use DateTime.UtcNow in your backend. First advantage is that you don't have to care about timezones and second, you don't have to do any calculations for Daylight Saving Times (read more in this article). .NET gives you great capabilities where you don't have to care much about the two points mentioned above. On the other side, you should never display the UTC time in your frontend. This can annoy your users, but here come the capabilities of .NET into play. If you have an UTC date you can easily convert it local time for displaying it to your user in the following way:

DateTime convertedDate = DateTime.SpecifyKind(
DateTime.Parse(dateStr),
DateTimeKind.Utc);
DateTime dt = convertedDate.ToLocalTime();

The snippet is taken from Drew Noakes answer on this thread, which is a great discussion about how you can convert your dates to local time.

EDIT

Regarding your edit: Yes, you have to do this. Otherwise the date will be stored in local time.



Related Topics



Leave a reply



Submit