0

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?

full_prog_full
  • 1,139
  • 10
  • 17
  • 3
    How do you display your `DateTime` exactly as an output? Can you please show your work as well? – Soner Gönül Dec 07 '15 at 14:15
  • Please show us the code where you are passing the `DateTime` back up to the UI – Jamie Rees Dec 07 '15 at 14:19
  • Come on guys, can you please wait _at least_ to see the minimal but complete code before you answer it? There can be a lot of reason. I didn't downvote you but we need to see OP's code to give complete answer in my humble opinion. – Soner Gönül Dec 07 '15 at 14:24
  • 1
    `DateTime.ToString()` returns format corresponding to your server side culture. Looks like you have english culture or similar – Gene R Dec 07 '15 at 14:26

4 Answers4

3

The output formats depend on the default settings of your culture. See this post for a related question.

Community
  • 1
  • 1
Sascha
  • 1,210
  • 1
  • 17
  • 33
  • SOLVED. In my Global.asax file: `System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(Request.UserLanguages[0]);` This essentially means that DateTimes are displayed as per user browser settings. – full_prog_full Dec 07 '15 at 15:04
1

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).

Mark Shevchenko
  • 7,937
  • 1
  • 25
  • 29
0

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.

BHR
  • 193
  • 1
  • 9
-1

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.

C. Knight
  • 739
  • 2
  • 5
  • 20