0

I'm building an app with ASP.NET MVC. Currently, the users create records. I store the date/time the records were created in UTC time. When I display them to the user, I want to format them like February 8, 2015 at 9:30 pm (local time). How do I do that in my view? Currently, I have:

@(Model.CreatedOnUTC.ToLongDateString() + " at " + Model.CreatedOnUTC.ToShortTimeString())

However, this isn't quite right.First, its not converting to the user's local time. And second, the format isn't correct. I know the server is not guaranteed to know the user's time zone. Let's assume it does though.

user70192
  • 13,786
  • 51
  • 160
  • 240

1 Answers1

1

I solved a pretty similar requirement with moment.js:

In the view:

<div class="utcdate-tolocaldate"> 
    @Html.Raw(Model.CreatedAt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'"))
</div>

and then with a function:

function utcConversion() {
   $('.utcdate-tolocaldate').each(function () {
       $(this).html(moment($(this).text()).format('L, LT'));
   });
   $('.utcdate-tolocaldateNoTime').each(function () {
       $(this).html(moment($(this).text()).format('L'));
   });
}

and here is the link for formatting:

less
  • 699
  • 6
  • 25