Created
April 3, 2013 07:55
-
-
Save sebnilsson/5299285 to your computer and use it in GitHub Desktop.
Format C# UTC DateTime to Local Format in JavaScript with Moment.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="dates"> | |
@for(var date in Model.Dates) { | |
<div class="date" data-utc="@date.GetEpochTicks()">@date.ToString() UTC</div> | |
} | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In meta tag like
http-equiv="last-modified"
the format is 2013-10-23@17:23:00 UTC, is it possible?