Reputation: 1189
I'm working on a large asp.net MVC web application.
A given message's creation date is stored in the (MS SQL) database as:
2015-12-02 18:08:24.383
Yet, somehow it is outputted to the web browser as a am/pm date:
12/2/2015 6:08:24 PM
Why is this? There doesn't seem to be any explicit conversions going on in the controller methods or views. The model value is:
public System.DateTime MessageSentDate { get; set; }
Then in the View:
This message was sent on: @message.MessageSentDate
As you can see, there's no conversion going on here. So why is a 24hr DateTime in the database being output as an am/pm value. Am I just missing something?
I would like to output 24hr format without am or pm. Is there a configuration value that can be assigned to change time display format across the whole app?
Upvotes: 0
Views: 1791
Reputation: 8197
Usually the output format of your values depends on regional settings.
The regional settings of web applications are defined in globalization
section of Web.config
:
<globalization uiCulture="en-IE" enableClientBasedCulture="false" />
Normally, en-US culture includes am/pm indicator. To avoid it you can use another culture (f.e. en-IE) or you can define the format without am/pm indicator inside your views, like here:
This @dt will be shown with default format.
But this @Html.FormatValue(dt, "{0:HH:mm:ss}") will be shown without am/pm.
Also, you can use @(dt.toString("HH:mm:ss")). Parentheses can help to
Razor to parse complex code.
HH means 24-hour... hours.
When enableClientBasedCulture
is set to false, the format will not depend on user's browser settings (I mean Accept-Language
HTTP header).
Upvotes: 1
Reputation: 193
in your controller you can format the date like this:
CreatedDate.ToString("M/d/yyyy HH:mm");
this will give you a 24 military time format.
Upvotes: 0
Reputation: 749
My guess would be that this is due to the culture which sets the Short and Long date formats. I know that in winforms this can be fairly easily overriden, not sure about MVC though.
Upvotes: -1