Skip to content

Instantly share code, notes, and snippets.

@Zimmergren
Last active August 29, 2015 14:14
Show Gist options
  • Save Zimmergren/b67b46e96b328927cebc to your computer and use it in GitHub Desktop.
Save Zimmergren/b67b46e96b328927cebc to your computer and use it in GitHub Desktop.
@model IEnumerable<Zimmergren.O365.MyContent.Controllers.SampleModel.Exchange.Message>
@{
ViewBag.Title = "Exchange Sample";
}
<h2>@ViewBag.Title.</h2>
<h3>Recent emails</h3>
<table class="table table-bordered table-striped">
<tr>
<th>
Subject
</th>
<th>
From
</th>
<th>
Received
</th>
</tr>
@{
//To display dates and times using the user's local timezone:
// * On the server-side, calculate the number of milliseconds elapsed since the Date epoch
// used by JavaScript.
// * In JavaScript, convert the milliseconds to a timezone-aware string.
DateTime JavaScriptDateEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
}
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Subject)
</td>
<td>
@Html.DisplayFor(modelItem => item.From.Name)
</td>
<td>
@* Create an empty span, tagged with a "data-datetime" attribute representing the
milliseconds since JavaScripts epoch time.
A JavaScript function (below) will then convert the attribute into a timezone-aware string. *@
<span data-datetime="@item.DateTimeReceived.Subtract(JavaScriptDateEpoch).TotalMilliseconds"></span>
</td>
</tr>
}
</table>
<script type="text/javascript">
// Finds all spans tagged with a "data-datetime" attribute, and sets their texts
// based on the user's local timezone and locale.
var allSpans = document.getElementsByTagName("span");
for (var i = 0; i < allSpans.length ; i++) {
var span = allSpans[i];
if (span.attributes["data-datetime"]) {
var date = new Date(0); // Initialize the Date object to the epoch.
date.setUTCMilliseconds(span.attributes["data-datetime"].value);
span.textContent = date.toLocaleString();
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment