2011-05-11 50 views
9

我们有一个MVC项目,我需要显示转​​换为用户本地时间的UTC日期。在我的模型我传递的UTC日期和在视图中我试图做到以下几点:MVC - UTC日期到LocalTime

<%: Html.DisplayFor(m=> m.SomeDate.ToLocalTime()) %> 

这会抛出异常。任何人都可以指示我如何将UTC日期转换为本地日期时间以便在客户端显示。我们将把日期存储为UTC,并且在显示时间这些日期将需要转换为本地机器等效。

+0

参见相关:[http://stackoverflow.com/questions/4066275/c-utc-to-users-local-time](http://stackoverflow.com/questions/4066275/c-utc -to-users-local-time) – 2011-05-11 02:49:12

+0

你的代码是服务器端而不是客户端。我意识到这是在下面的评论标记,但这是一个受欢迎的问题,它的方式不清楚它的要求! – noelicus 2017-06-10 08:45:08

回答

7
DateTime now = DateTime.UtcNow; 
DateTime localNow = TimeZoneInfo.ConvertTimeFromUtc(now, TimeZoneInfo.Local); 
+0

这将需要在服务器端完成,而我在谈论客户端 – Amitesh 2011-05-11 02:50:23

+3

@Amitesh,在你的问题的例子中,你也试图在服务器端转换日期。 – 2011-05-11 02:52:09

+0

我试图将日期转换为本地客户端使用 <%:Html.DisplayFor(m => m.SomeDate.ToLocalTime())%>。 – Amitesh 2011-05-11 03:05:48

9

您将需要存储的用户的时区服务器端,然后使用这样的事情(尽管它应该在控制器,而不是视做):

@TimeZoneInfo.ConvertTimeFromUtc(Model.CreatedOn, TimeZoneInfo.FindSystemTimeZoneById("E. Australia Standard Time")) 
2

感觉就像是位的一个kludge,但这工作在MVC3客户端

@DateTime.Parse(Html.DisplayFor(m=> m.SomeDate).ToString()).ToLocalTime().ToString() 
+0

我喜欢它。它很干净,就坐在那里。如果你想格式化不同的视图,很容易 - 很好地完成。它只是看起来像一个杂食。 – 2016-10-26 14:19:42

+3

这看起来对我来说所有的服务器端.... ?? – noelicus 2017-06-07 09:26:15

4

在mvc中,您可以通过操作过滤器来解决此问题。 请使用以下步骤:
1)在会话中存储客户端时区偏移量信息。
2)创建DatetimeConverter助手类。

public class DateTimeConverter 
{ 
    public static DateTime? ToLocalDatetime(DateTime? serverDate, int offset)  
    { 
     if (serverDate == null) return null; 
     return serverDate.Value.AddMinutes(offset * -1); 
    } 

} 

3)。创建动作过滤器。

public class LocalDateTimeConverter : ActionFilterAttribute 
{ 
    public override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     var model = filterContext.Controller.ViewData.Model; 
     if (model != null && filterContext.HttpContext.Session["LocalTimeZoneOffset"] != null) 
      ProcessDateTimeProperties(model, filterContext); 
     base.OnActionExecuted(filterContext); 
    } 

    private void ProcessDateTimeProperties(object obj, ActionExecutedContext filterContext) 
    { 
     if (obj.GetType().IsGenericType) 
     { 
      foreach (var item in (IList)obj) 
      { 
       ProcessDateTimeProperties(item, filterContext); 
      } 
     } 
     else 
     { 
      TypeAccessor member; 
      List<PropertyInfo> props = new List<PropertyInfo>(); 
      props.AddRange(obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty).ToList()); 
      member = TypeAccessor.Create(obj.GetType()); 
      foreach (PropertyInfo propertyInfo in props) 
      { 
       if (propertyInfo.PropertyType == typeof(DateTime) || propertyInfo.PropertyType == typeof(DateTime?)) 
       { 
        { 
         member[obj, propertyInfo.Name] = DateTimeConverter.ToLocalDatetime((DateTime?)propertyInfo.GetValue(obj), ((int)filterContext.HttpContext.Session["LocalTimeZoneOffset"])); 
        } 
       } 
       else if (propertyInfo.PropertyType.IsGenericType && propertyInfo.GetValue(obj) != null) 
       { 
        foreach (var item in (IList)propertyInfo.GetValue(obj)) 
        { 
         ProcessDateTimeProperties(item, filterContext); 
        } 
       } 
      } 
     } 
    } 
} 

4)。应用LocalDateTimeConverter过滤器包含要返回视图的模型数据。

经过这些步骤后,您可以在视图中看到包含转换为本地dateTime的dateTime信息的结果。

0

使用此代码为UTC时间转换为本地时间

<%: Html.DisplayFor(m=> m.SomeDate.ToLocalTime().ToString()) %> 

可以在剃刀

@Html.DisplayFor(m=> m.SomeDate.ToLocalTime().ToString()) 
+0

我不认为这有效。您将得到一个异常:System.InvalidOperationException:'模板只能用于字段访问,属性访问,单维数组索引或单参数自定义索引器表达式。 – mikecamimo 2017-08-27 23:02:10

0

所有好的答案使用使用下面的代码。我做我的是这样的:

@Html.DisplayFor(m=> m.SomeDate.ToLocalTime().ToString(CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern + " " + CultureInfo.CurrentUICulture.DateTimeFormat.LongTimePattern))

0

转换UTC日期时间可以LOCALDATE使用jQuery以及完成。使用jquery的主要好处是你可以在azure上托管你的网站。上面给出的一些方法将不起作用。只有一个选项会被用于使用jquery/javascript。由于Datetime.Now如果您的网站托管在Azure上,将返回utc timetimetime.now.tolocaltime()。请在jquery中找到一个例子来将UTC时间转换为localdatetime。

var date = new Date('2/8/2018 3:57:48 PM UTC'); 
date.toString() // "Thu Feb 08 2018 21:27:48 GMT+0530 (India Standard Time)"