Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Displaying date/time using the user timezone in ASP.NET MVC

, 23 Nov 2012
Rate this:
Please Sign up or sign in to vote.
This post will show you how you can display date/time using the user timezone (without it being configured anywhere in your application). Storaging dates in your data source The first thing you have to do is to make sure that all dates are stored using Universal Cordinated Time (UTC). The easiest wa

This post will show you how you can display date/time using the user timezone (without it being configured anywhere in your application).

Storaging dates in your data source

The first thing you have to do is to make sure that all dates are stored using Universal Cordinated Time (UTC).

The easiest way to do that is to convert it like this:

var dbDate = myDate.ToUniversalTime();

When loading dates simply do no conversion at all.

Displaying the date/time

The date/time can not be formatted server-side since the HTTP request contains no information about the time zone. Instead we have to do it client side (or start using AJAX to configure server side).

Hence we need to push the date to the client in a way that would allow us to reformat it. The easiest way to do that is to create a display template. The great thing with that is that all date/times will automatically be reformatted if you are using Html.DisplayFor.

Create a template named /Views/Shared/DisplayTemplates/DateTime.cshtml with the following content:

@model DateTime
<span data-date="@Model.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds">@Model.ToString()</span>

We simply display the date as UTC. But we do also include the unix date in an attribute. We’ll use that to do the formatting.

Formatting the date/time

Now we only have the formatting left. For that we’ll use a combination of standard javascript and jQuery.

Add this somewhere in your application:

<script type="text/javascript">
$(function(){
    $('[data-date]', container).each(function () {
        // the date construct will automatically convert to local time
        var localDate = new Date(parseInt($(this).attr('data-date')));

        //display time only for today, otherwise only the date.
        // feel free to use your own logic
        var now = new Date();
        if (localDate.getDate() == now.getDate() && localDate.getFullYear() == now.getFullYear() && now.getMonth() == localDate.getMonth()) {
            $(this).html(localDate.toLocaleTimeString());
        } else {
            $(this).html(localDate.toLocaleDateString());
        }
    });
});
</script>

End result

Done.

The post Displaying date/time using the user timezone in ASP.NET MVC appeared first on jgauffin's coding den.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

jgauffin
Founder Gauffin Interactive AB
Sweden Sweden
Founder of OneTrueError, a .NET service which captures, analyzes and provide possible solutions for exceptions.
 
blog | twitter
Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 23 Nov 2012
Article Copyright 2012 by jgauffin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid