Skip to content

Instantly share code, notes, and snippets.

@sebnilsson
Created April 3, 2013 07:55
Show Gist options
  • Save sebnilsson/5299285 to your computer and use it in GitHub Desktop.
Save sebnilsson/5299285 to your computer and use it in GitHub Desktop.
Format C# UTC DateTime to Local Format in JavaScript with Moment.js
private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static double GetEpochTicks(this DateTime dateTime)
{
return dateTime.Subtract(Epoch).TotalMilliseconds;
}
/// <reference path="~/Libraries/moment-2.0.0.js" />
$('.date').each(function() {
var $this = $(this), utcDate, local, formattedDate;
utcDate = parseInt($this.attr('data-utc'), 10) || 0;
local = moment.utc(utcDate).local();
formattedDate = local.format('YYYY-MM-DD HH:mm');
$this.text(formattedDate);
});
<div class="dates">
@for(var date in Model.Dates) {
<div class="date" data-utc="@date.GetEpochTicks()">@date.ToString() UTC</div>
}
</div>
@kiquenet
Copy link

kiquenet commented Sep 8, 2017

In meta tag like http-equiv="last-modified" the format is 2013-10-23@17:23:00 UTC, is it possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment