簡體   English   中英

在ASP.NET MVC中將時間格式更改為24小時(軍事時間)

[英]Change time format to 24hr (military time) in ASP.NET MVC

我的ASP.NET MVC 5應用程序當前以12小時格式顯示時間。 如何更改為以24小時格式顯示時間?

我已經看到了這個問題 ,但是我可以在應用程序級別上做任何事情來應用此問題嗎? 我的意思是也許在global.asax或web.config中添加一些內容,這些內容會更改整個應用程序的時間格式?

嘗試這種方式,在Global.asax.cs中添加Application_BeginRequest方法:

// using System.Globalization;
protected void Application_BeginRequest(object sender, EventArgs e)
{
    CultureInfo ci = new CultureInfo("en-US");
    ci.DateTimeFormat.ShortDatePattern = "MM dd yyyy";
    // there are a lot of other pattern properties in ci.DateTimeFormat you can set 
    System.Threading.Thread.CurrentThread.CurrentCulture = ci;
}

然后您調用DateTime.Now.ToShortDateString()並得到02 17 2016 (for Feb 17,2016)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM