Asp.net mvc5日期时间渲染格式 [英] Asp.net mvc5 datetime rendering format

查看:170
本文介绍了Asp.net mvc5日期时间渲染格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是模型:

e [DataType(DataType.Date)]

public Nullable< System.DateTime> PublishDate {get;组; }
这是视图:

e< dt>
@ Html.DisplayNameFor(model => model.PublishDate)
< / dt>

< dd>
@ Html.DisplayFor(model => model.PublishDate)
< / dd>
当我使用该页面的英文版本时,日期以这种方式呈现,例如2017年8月14日,当我用法语更改语言时,日期以这种方式呈现14.8.2017。为什么它很开心,任何人都可以帮助我,因为无论选择哪种语言,日期格式都应该是一样的。





< b>我尝试了什么:



i解释了上面的问题和我的解决方案。

解决方案

< blockquote>在追逐文化时试试这个。将newCulture设置为您改变的文化



 CultureInfo newCulture =(CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone(); 
newCulture.DateTimeFormat.ShortDatePattern =dd-MMM-yyyy;
newCulture.DateTimeFormat.DateSeparator = - ;
Thread.CurrentThread.CurrentCulture = newCulture;













替换为此代码



 protected void Application_AcquireRequestState(object sender,EventArgs e)
{
//检查会话对象是否准备就绪很重要
if(HttpContext.Current.Session!= null)
{
CultureInfo ci =(CultureInfo)this.Session [ Culturee];
//首先检查会话中是否没有值
//并设置默认语言
//这可能发生在第一个用户的请求
if(ci == null)
{
//将默认文化设置为英语不变量
string langName =en;

//尝试从Accept lang HTTP头获取值
if(HttpContext.Current.Request.UserLanguages!= null&&
HttpContext.Current.Request.UserLanguages .Length!= 0)
{
//获取接受列表
langName = HttpContext.Current.Request.UserLanguages [0] .Substring(0,2);
}
ci = new CultureInfo(langName);
this.Session [Culturee] = ci;
}
//最后为每个请求设置文化

ci.DateTimeFormat.ShortDatePattern =dd-MMM-yyyy;
ci.DateTimeFormat.DateSeparator = - ;
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = ci;

//Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);



}
}


如果要控制特定格式字段,使用 DisplayFormat 属性:

DisplayFormatAttribute类| Microsoft Docs [ ^ ]

 [DisplayFormat(  dd-MMM-yyyy)] 
public DateTime? PublishDate { get ; set ; }

NB: DateTime?是编写 Nullable< DateTime> <的简写方式/ code>。)



如果要覆盖所有日期的格式,请在 Views \Shared中创建显示模板\ DisplayTemplates\Date.cshtml

 @model DateTime? 
@if(Model.HasValue)
{
@ Model.Value.ToString(dd-MMM-yyyy)
}

然后添加任何应该只显示日期的属性的 DataType 属性:

 [DataType(DataType.Date)] 
public DateTime? PublishDate { get ; set ; }





如果要关闭整个应用程序的自动全球化,可以更改 web.config 文件。您可能将文化设置为自动:

< configuration> 
< system.web>
< globalization culture =autouiCulture =auto/>
< /system.web>
< / configuration>

这会根据用户的首选语言设置文化。



将auto替换为代码您想要使用的文化。例如:

< configuration> 
< system.web>
< globalization culture =en-GBuiCulture =en-GB/>
< /system.web>
< / configuration>



您甚至可以为文化和UI文化设置不同的设置 - 文化驱动数字和日期格式,货币符号,排序等UI文化主要用于确定哪些 .resx 文件用于查找本地化资源。



在ASP中自动选择文化进行本地化。 NET - Rick Strahl的网络日志 [ ^ ]


This is the model :

e[DataType(DataType.Date)]

    public Nullable<System.DateTime> PublishDate { get; set; }
This is the view :

e   <dt>
            @Html.DisplayNameFor(model => model.PublishDate)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.PublishDate)
        </dd>
When I am on English version of the page , date renders in this way for example 14/08/2017 , and when i change language in French then date renders in this way 14.8.2017 . Why it is happining that , can anyone help me , for no matter language selected , the date format should always be the same .



What I have tried:

i explained the problem and my solution above.

解决方案

Try this while chaging the culture. Set newCulture as your changed culture

CultureInfo newCulture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
newCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";
newCulture.DateTimeFormat.DateSeparator = "-";
Thread.CurrentThread.CurrentCulture = newCulture;







Replace with this code

protected void Application_AcquireRequestState(object sender, EventArgs e)
{
//It's important to check whether session object is ready
if (HttpContext.Current.Session != null)
{
CultureInfo ci = (CultureInfo)this.Session["Culturee"];
//Checking first if there is no value in session 
//and set default language 
//this can happen for first user's request
if (ci == null)
{
//Sets default culture to english invariant
string langName = "en";

//Try to get values from Accept lang HTTP header
if (HttpContext.Current.Request.UserLanguages != null && 
HttpContext.Current.Request.UserLanguages.Length != 0)
{
//Gets accepted list 
langName = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2);
}
ci = new CultureInfo(langName);
this.Session["Culturee"] = ci;
}
//Finally setting culture for each request

ci.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";
ci.DateTimeFormat.DateSeparator = "-";
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = ci;

//Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);


  
}
}


If you want to control the format for a specific field, use the DisplayFormat attribute:
DisplayFormatAttribute Class | Microsoft Docs[^]

[DisplayFormat("dd-MMM-yyyy")]
public DateTime? PublishDate { get; set; }

(NB: DateTime? is a shorthand way of writing Nullable<DateTime>.)

If you want to override the format for all dates, create a display template in Views\Shared\DisplayTemplates\Date.cshtml:

@model DateTime?
@if (Model.HasValue)
{
    @Model.Value.ToString("dd-MMM-yyyy")
}

and then add the DataType attribute to any properties which should only show the date:

[DataType(DataType.Date)]
public DateTime? PublishDate { get; set; }



If you want to turn off automatic globalization for the entire application, you can change the web.config file. You probably have the culture set to "auto":

<configuration>
  <system.web>
    <globalization culture="auto" uiCulture="auto" />
  </system.web>
</configuration>

This sets the culture based on the user's preferred language.

Replace "auto" with the code of the culture you want to use. For example:

<configuration>
  <system.web>
    <globalization culture="en-GB" uiCulture="en-GB" />
  </system.web>
</configuration>


You can even have different settings for culture and UI culture - the culture drives number and date formatting, currency symbols, sorting, etc., whereas the UI culture is primarily used to determine which .resx files are used to look up localized resources.

Auto Selecting Cultures for Localization in ASP.NET - Rick Strahl's Web Log[^]


这篇关于Asp.net mvc5日期时间渲染格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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