There are a couple options:
- Have a setting per users which indicates the users timezone and convert the times on every view/page render
- Have the user's browser handle the conversion
<time datetime="@Html.DisplayFor(modelItem => item.Created) UTC"> @Html.DisplayFor(modelItem => item.Created) UTC </time>We can iterate over all of the time elements and convert the UTC times and leverage javascript to translate the times to local with the following code.
function UtcTimesToLocal() { $("time").each(function (index, element) { var el = $(element), time = el.attr("datetime"), converted = new Date(time); var dateString = ToDateString(converted); el.text(dateString); }); } function ToDateString(dateObj, dateFormat) { var curr_year = dateObj.getFullYear(), curr_month = LeadingZero(dateObj.getMonth() + 1), curr_date = LeadingZero(dateObj.getDate()), curr_hour = LeadingZero(dateObj.getHours()), curr_min = LeadingZero(dateObj.getMinutes()), curr_sec = LeadingZero(dateObj.getSeconds()), curr_ampm = "AM"; if (curr_hour > 11) { curr_ampm = "PM"; curr_hour = (curr_hour == 12) ? 12 : curr_hour - 12; } var timestamp = curr_year + "-" + curr_month + "-" + curr_date + " " + curr_hour + ":" + curr_min + ":" + curr_sec + " " + curr_ampm + " " + LocalTimeZone(); return timestamp; } function LeadingZero(val) { return (val < 10) ? "0" + val : val; } function LocalTimeZone () { // From http://stackoverflow.com/questions/2897478/get-client-timezone-not-gmt-offset-amount-in-js var now = new Date().toString(), timezone = now.indexOf('(') > -1 ? //now.match(/\([^\)]+\)/)[0] : // Uncomment this line to return the full time zone text now.match(/\([^\)]+\)/)[0].match(/[A-Z]/g).join('') : // Uncomment this line to return the full time zone abbreviation now.match(/[A-Z]{3,4}/)[0]; if (timezone == "GMT" && /(GMT\W*\d{4})/.test(now)) timezone = RegExp.$1; return timezone; }Then at the bottom of the page, run the
UtcTimesToLocal
function.
<script> $(function() { UtcTimesToLocal(); }); </script>An example project can be found on my UTCtoLocalJS GitHub repository.
No comments:
Post a Comment