-2

I've got the following String format: "26/10/2017 22:54:22"

im trying to convert it to DateTime object, which should be of the same format as the string (24hours format) but somewhy it generates a 12h format.

my code:

var time = "26/10/2017 22:54:22";
            DateTime convertedDate = DateTime.ParseExact(time, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
            Console.WriteLine(convertedDate);
            Console.ReadLine();

the output i get is: 10/26/2017 10:54:22 PM

any ideas why is that?

gil
  • 2,388
  • 1
  • 21
  • 29
  • 4
    because your normal date format is month/day/year 12h clock? – BugFinder Oct 26 '17 at 07:55
  • 2
    `Console.WriteLine` outputs depending on current culture settings. Check if your machine uses American date with 12h format. – Tetsuya Yamamoto Oct 26 '17 at 07:57
  • @TetsuyaYamamoto isn't this rather an answer than a comment? – Antonín Procházka Oct 26 '17 at 07:58
  • you can simply force the datetime to use your format: `Console.WriteLine(convertedDate.ToString("MM/dd/yyyy HH:mm:ss"))` – Mong Zhu Oct 26 '17 at 08:00
  • @TetsuyaYamamoto is there anything i can do to prevent it? not to depend on culture settings? – gil Oct 26 '17 at 08:00
  • You can use date-time format string manipulation either by `ToString` or `string.Format` with specified formatting. See https://stackoverflow.com/questions/24277622/interpreting-dates-console-writeline-vs-string-format for similar issue. – Tetsuya Yamamoto Oct 26 '17 at 08:03
  • There are a ton of answers to this question already.... did you even bother to look? – James Oct 26 '17 at 08:03
  • @MongZhu my goal isn't to convert it to a string. i want it to be a DateTime object with the format i mentioned – gil Oct 26 '17 at 08:04
  • 3
    _"i want it to be a DateTime object with the format i mentioned"_ `DateTime` objects _don't have_ formats. They represent a moment in time. How you choose to print it out on screen is up to you. – James Thorpe Oct 26 '17 at 08:05
  • `DateTime` itself is a struct, which has no specific format provided. What you can do is setting `Thread.CurrentThread.CurrentCulture` if you don't want to change machine's regional settings. – Tetsuya Yamamoto Oct 26 '17 at 08:05
  • @gilleibovitz see the comment by James Thorpe – Mong Zhu Oct 26 '17 at 08:11

2 Answers2

3

DateTime by itself doesn't make any difference if the time is 12-hour or 24-hour, it's only when converted to a string that there is a difference.

Console.WriteLine will call the ToString() method (the one without any parameters) of the object passed as parameter. For a DateTime, ToString() formats the date and time according to the current locale. Yours is probably set to a 12-hour format.

If you need to print your time with a different format, you should format the DateTime explicitely, using another variant of the ToString method.

In your case:

Console.WriteLine(convertedDate.ToString("MM/dd/yyyy HH:mm:ss"));
Najkin
  • 932
  • 1
  • 7
  • 16
1

If you have successfully parsed into a DateTime structure, there is overloaded method of ToString for DateTime, which is converts the value of the current DateTime object to its equivalent string representation using the specified format and the formatting conventions of the current culture.

So in your case you can just use:

Console.WriteLine(convertedDate.ToString("MM/dd/yyyy HH:mm:ss"));

MSDN Link: DateTime.ToString Method (String)

SᴇM
  • 7,024
  • 3
  • 24
  • 41