0

I am coding a MVC 5 internet application that is being deployed to an Azure website. This application is going to be used internationally, and I am wanting to know if storing any DateTime values should use UTC time?

Here is an example: I have an object that has a start date time value as well as an end date time value. These values are set by a user and are used to determine if data should be shown to a user. If the current date time value is between the start date time and end date time, then data is shown.

Because my application is going to be used internationally, and there are different time zones, should I store the start date time value and end date time value as a DateTime object with UTC, or a standard DateTime value would be all that is needed? Do I need to worry about setting and displaying DateTime values for different time zones, or is this incorporated into the DateTime object?

Thanks in advance.

EDIT

How about when editing a DateTime that is stored as UTC.

If I have a DateTime that is stored as UTC in an object, and the user wants to change this value, do I need to convert the DateTime (that is displayed as .ToLocalTime()) back to a UTC DateTime when I store this value back in the database after it has been edited?

EDIT2

Can you have a quick look at these functions that I have coded to ensure the coding is correct?

public DateTime ConvertUTCDateTimeToLocalTime(DateTime utcDateTime)
{
    DateTime convertedDate = DateTime.SpecifyKind(utcDateTime, DateTimeKind.Utc);
    return convertedDate.ToLocalTime();
}

public DateTime ConvertLocalDateTimeToUniversalTime(DateTime localDateTime)
{
    DateTime convertedDate = DateTime.SpecifyKind(localDateTime, DateTimeKind.Local);
    return convertedDate.ToUniversalTime();
}

public DateTime ConvertUTCDateTimeToLocalTime(string utcDateTime)
{
    DateTime convertedDate = DateTime.SpecifyKind(DateTime.Parse(utcDateTime.ToString()), DateTimeKind.Utc);
    return convertedDate.ToLocalTime();
}

public DateTime ConvertLocalDateTimeToUniversalTime(string localDateTime)
{
    DateTime convertedDate = DateTime.SpecifyKind(DateTime.Parse(localDateTime.ToString()), DateTimeKind.Local);
    return convertedDate.ToUniversalTime();
}
Simon
  • 7,991
  • 21
  • 83
  • 163

1 Answers1

2

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.

Community
  • 1
  • 1
Horizon_Net
  • 5,959
  • 4
  • 31
  • 34
  • So I should store all DateTime values as DateTime with UTC, and when I wish to display any DateTime values to users in any location, use the DateTime.ToLocalTime()? Is that correct? – Simon Jan 24 '15 at 09:27
  • Yes, everything that hits your storage system should be in UTC format and everything visible to your user should be displayed in their local time (otherwise it can become confusing for them). – Horizon_Net Jan 24 '15 at 09:49
  • Do I need to worry about the Kind property at all if I am only storing DateTime.UTC values and displaying these values with DateTime.ToLocalTime()? – Simon Jan 24 '15 at 13:04
  • Yes, you should always state that this date is in UTC format (as shown in the code snippet in my answer). It will also make things more clear when reading your code in some point in the future. – Horizon_Net Jan 24 '15 at 14:11
  • Seems to be ok on a first glance. Have you tried it? – Horizon_Net Jan 27 '15 at 08:18
  • Yes I have, and the correct values are being displayed. I just wanted a second person to ensure that it is all coded correctly. Thanks for your help. – Simon Jan 29 '15 at 02:30