15

I have an ASP.NET MVC app that stores all SQL DateTime in UTC, so that the time is consistent regardless of time zone that the client is hitting the site from.

Now, I want to re-display the correct time to the user, so any time I display a time in my View I use:

timeVariable.ToLocalTime();

However, .ToLocalTime() is based on the server, not the client.

Do I need to handle this in JavaScript on the client?

I probably could pass the time zone as part of the request and have the controller handle the offest, but I am assuming there is a better way to do this. Or isn't there?

Thanks in advance for any help!

-Matt

Derrick
  • 2,502
  • 2
  • 24
  • 34
MattW
  • 12,902
  • 5
  • 38
  • 65
  • 1
    Take a look at this post I think it's the same scenario... http://stackoverflow.com/questions/671964/asp-net-client-time – Marko Nov 15 '11 at 22:32

3 Answers3

14

For Asp.Net MVC + jQuery I've created a DisplayTemplate and script to help with this.

Model class:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

public class SampleModelWithDate
{
    [UIHint("UTCTime")]
    [DataType(DataType.DateTime)]
    public DateTime Date { get; set; }
}

DisplayTemplate: Views\Shared\DisplayTemplates\UTCTime.cshtml

@model DateTime
<span class="UTCTime">@Model</span>

Add to Views\Shared_Layout.cshtml, or to your View:

<script>
    $(function () {
        // Determine timezone offset in milliseconds
        // from: http://www.w3schools.com/jsref/jsref_getTimezoneOffset.asp
        var d = new Date()
        var offsetms = d.getTimezoneOffset() * 60 * 1000;
        $('.UTCTime').each(function () {
            try
            {
                var text = $(this).html();

                var n = new Date(text);
                n = new Date(n.valueOf() - offsetms);

                $(this).html(n.toDateString() + " " + n.toLocaleTimeString());

                $(this).attr("Converted from UTC " + text);
            }
            catch (ex) {
                console.warn("Error converting time", ex);
            }
        });
    });
</script>

Clean it up a little of course, it's a bit verbose so you can see what's going on.

Hugh Jeffner
  • 2,936
  • 4
  • 32
  • 31
Derrick
  • 2,502
  • 2
  • 24
  • 34
8

Almost all sites that I have seen that translate time to local time ask the user to specify his or her timezone when creating an account. Gmail is one example but there are many others.

I've found that many solutions trying to use javascript or reverse-lookup on the IP address are prone to failure and edge cases.

Mike Marshall
  • 7,788
  • 4
  • 39
  • 63
  • Many applications are smart enough to determine the timezone that device is in so that when you travel, local time is adjusted automatically. An account setting wouldn't accommodate that, but it is a good start. – Daniel Przybylski Nov 01 '19 at 22:18
4

In addition to Derrick's answer, "Append 'UTC' to the string before converting it to a date in javascript." Convert UTC date time to local date time using JavaScript

The UTCTime can be converted automatically to local time.

<script>
    $(function () {
        var d = new Date()
        $('.UTCTime').each(function () {
            try
            {
                var text = $(this).html() + ' UTC'; //Append ' UTC'

                var n = new Date(text);

                $(this).html(n.toDateString() + " " + n.toLocaleTimeString());

                $(this).attr("title", "Converted from UTC " + text);
            }
            catch (ex) {
                console.warn("Error converting time", ex);
            }
        });
    });
</script>
Community
  • 1
  • 1
stenlytw
  • 938
  • 13
  • 18