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
1 2 3 | < time datetime="@Html.DisplayFor(modelItem => item.Created) UTC"> @Html.DisplayFor(modelItem => item.Created) UTC </ time > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 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 () { 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; } |
UtcTimesToLocal
function.
1 2 3 | < script > $(function() { UtcTimesToLocal(); }); </ script > |
No comments:
Post a Comment