MyUtcDateTimeProperty
DateTime
@Html.DisplayFor(m => m.MyUtcDateTimeProperty)
_Layout.cshtml
$(document).ready(function () { var dt = new Date(); var tzo = dt.getTimezoneOffset(); document.cookie = "timeZoneOffset=" + tzo + "; path=/"; });
public class ClientTimeZoneAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { var cookie = filterContext.HttpContext.Request.Cookies["timeZoneOffset"]; Int16 n = 0; var tzo = (cookie != null && Int16.TryParse(cookie.Value, out n) ? n : 0); filterContext.HttpContext.Session["tzo"] = tzo; base.OnActionExecuting(filterContext); } }
FilterConfig
RegisterGlobalFilters
OnActionExecuting
filters.Add(new ClientTimeZoneAttribute());
public class ClientTimeZoneHelper { public static string ConvertToLocalTimeAndFormat(DateTime dt, string format) { var o = HttpContext.Current.Session["tzo"]; var tzo = o == null ? 0 : Convert.ToDouble(o); dt = dt.AddMinutes(-1 * tzo); var s = dt.ToString(format); if (tzo == 0) s += " GMT"; return s; } }
/Views/Shared/DisplayTemplates
@model System.DateTime @My.Namespace.ClientTimeZoneHelper.ConvertToLocalTimeAndFormat(Model, "MM/dd/yy h:mm:ss tt")
ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.