更改剃须刀的默认DateTime格式 [英] Change default DateTime format for razor

查看:77
本文介绍了更改剃须刀的默认DateTime格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以将所有DateTime对象的默认格式更改为例如在剃刀中显示 dd.MM.yyyy?

Is there a way I can change the default formatting for all DateTime objects to e.g. "dd.MM.yyyy" in Razor ?

我想介绍一下程序员忘记调用扩展方法或传递格式字符串(例如<$)的情况。 c $ c>< p> @ Model.MyDateTimeObject< / p> 。

I would like to cover cases when a programmer forgets to call an extension method or to pass a format string, i.e. <p>@Model.MyDateTimeObject</p>.

推荐答案

有就像 Razor中的默认格式一样。当您这样做时:< p> @ Model.MyDateTimeObject< / p> Razor将只使用默认的 DateTime.ToString 重载以打印日期。

There is no thing like default formatting in Razor. When you do this: <p>@Model.MyDateTimeObject</p> Razor will simply use the default DateTime.ToString overload to print the date.

区域性信息,例如,如果您希望您的示例作为默认的ToString行为,则应更改实际请求的当前区域性。例如。在global.asax中:

This works by the actual culture information, so for example if you want your example as the default ToString behavior then you should change the current culture for the actual request. E.g. in global.asax:

protected void Application_BeginRequest()
{
    CultureInfo culture = (CultureInfo)CultureInfo.InvariantCulture.Clone();
    culture.DateTimeFormat.ShortDatePattern = "dd.MM.yyyy";
    culture.DateTimeFormat.LongTimePattern = "";
    Thread.CurrentThread.CurrentCulture = culture;
}

这篇关于更改剃须刀的默认DateTime格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆