R K Sharma
R K Sharma

Reputation: 855

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

My ASP.NET MVC 5 application currently displays times in 12 hour format. How can I change this to display times in 24 hour format?

I have seen this question, but is there anything I can do on an application level to apply this? I mean maybe add something in global.asax or web.config which will change the time format throughout the application?

Upvotes: 0

Views: 1138

Answers (1)

Ricky
Ricky

Reputation: 10751

Try out this way, add Application_BeginRequest method in Global.asax.cs:

// 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;
}

Then you invoke DateTime.Now.ToShortDateString() and will get 02 17 2016(for Feb 17, 2016)

Upvotes: 1

Related Questions