-2

I am trying to set the DateTime format to 24 hours. Originally I have a string with a 12 hour representation. All solutions I have found are converting DateTime to string.

string dateString = "Mon 16 Jun 8:30 AM 2008"; // <-- Valid
        string format = "dd/MM/yyyy HH:mm";
        DateTime dateTime;
        if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture,
            DateTimeStyles.RoundtripKind, out dateTime))
        {
            DateTime dateIn24 = dateTime;//  dateIn24 should be in 24 hour format
        }

Is there anything we can do in web.config? Like the following:

    <globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB"/>

i got the answer

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"); if its en-US 12 hour format by default it will be en-US based on system date time settings

Lijo
  • 6,498
  • 5
  • 49
  • 60
  • Take a look on http://stackoverflow.com/questions/8337625/convert-am-pm-time-to-24-hours-format – Amit Mishra Jan 11 '17 at 10:47
  • i have gone through it ..they are converting date to string ..i need to output as date time object – Lijo Jan 11 '17 at 10:49
  • 1
    DateTime has no format unless you are printing it as a string. Otherwise it has its internal representation which is the same "format" for all instances – pinkfloydx33 Jan 11 '17 at 10:55
  • @Lijo If you found a solution, you should answer your question yourself and mark your answer as the solution :/ –  Jan 11 '17 at 11:24
  • 1
    Still the solution you've found is misleading. There **is no** internal 12 hours or 24 hours representation. The default `ToString` implementation uses the current culture to format the `DateTime`. Hence you will get a 12 hour representation, when the `CurrentCulture` is en-US. This is - more or less - what I've written in my answer. – Paul Kertscher Jan 11 '17 at 11:24

2 Answers2

2

The DateTime instance only contains the information, and the Hour property is an integer from 0 to 23, according to MSDN documentation:

Property Value

Type: System.Int32

The hour component, expressed as a value between 0 and 23.

https://msdn.microsoft.com/library/system.datetime.hour(v=vs.110).aspx

If you're talking about formatting it, then you need to convert it to a string, like mentioned in the comments.

Community
  • 1
  • 1
Emerson Cardoso
  • 394
  • 3
  • 5
0

As far as I know, the 24-hour format is neither not a matter of how the DateTime is parsed, nor saved, but rather of the formatting. Given that you enter the if-block (I have not been successful on parsing youre dateString in LinqPad) you can obtain a correctly formatted date string with

var dateStringWith24Hours = dateTime.ToString(dateString);

since the HH in your format string means that you'd like to format the hours as 24 hours.

Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57
  • This is what I am talking about. Internally the `DateTime` will always have a 24 hour representation. The 12-hour format is only relevant when converting the `DateTime` to a string. Maybe you could elaborate *why* and *how* you need the 12-hour format and then we could propose a solution. – Paul Kertscher Jan 11 '17 at 11:08
  • by default date time is having 12 hour format . i need in 24 hour format – Lijo Jan 11 '17 at 11:11
  • To repeat myself: The `DateTime` structure does not know anything about a 12 hour representation. Only the respective formatting methods do. – Paul Kertscher Jan 11 '17 at 11:14
  • create simple windows form application check datetime.now formt – Lijo Jan 11 '17 at 11:15
  • "create simple windows form application check datetime.now formt" - when you do that, VS converts DateTime.Now to a string, and you see a representation of it in the IDE. Internally, it's only a set of variables containing the hours, minutes, seconds, etc. – Emerson Cardoso Jan 11 '17 at 11:27
  • Yeah, for me this will result in a 24 h representation (believe me), but this is only due to the implementation of `ToString`, not due to some internal representation. To display a `DateTime` as a string it has to be converted to - well - a `string`. This is done via `ToString`, which uses your current culture. – Paul Kertscher Jan 11 '17 at 11:27
  • 2
    @EmersonCardoso Thanks for your effort, but I do not think that he'll believe us... – Paul Kertscher Jan 11 '17 at 11:28
  • so you mean to say date time will always show 24 hour.please assign the date time to a date time variable and check....plz.. – Lijo Jan 12 '17 at 06:33
  • date we can format to string as we wish..but am talking about the date time culture ...even we can set the culture in iis globalization. – Lijo Jan 12 '17 at 06:34
  • Yes, the **textual** representation depends on the culture, I didn't state anything else. But the **internal** (numeric) representation is completely independent from the culture. – Paul Kertscher Jan 12 '17 at 06:38