17

How can I convert this 2014-01-01 23:00:00 to DateTime I have done like this:

Console.WriteLine(DateTime.ParseExact("2014-01-01 23:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));

and the result was like this :

1/1/2014 11:00:00 PM

this thing drive me crazy because this format was working in java.

danarj
  • 1,798
  • 7
  • 26
  • 54
  • 2
    Did you get this result by calling the ToString method? – Plue Jan 27 '14 at 14:13
  • 1
    does simply using `DateTime.Parse("2010-01-01 23:00:00")` not give you your expected DateTime? – Jonesopolis Jan 27 '14 at 14:13
  • You're not using a format string in your output, only in your input. – David Jan 27 '14 at 14:16
  • Looks fine to me. The `DateTime` parsed successfully. At that point, it doesn't *have* a format. You then (via `WriteLine`) forcefully converted it back into a string in whatever format the default is. – Damien_The_Unbeliever Jan 27 '14 at 14:16
  • See e.g. the [documentation](http://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx): "Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D... " – Damien_The_Unbeliever Jan 27 '14 at 14:22
  • Don't use ParseExact. Use instead Parse or TryParse. without specifying the colture. – Angelo Mascaro Aug 12 '19 at 12:58

2 Answers2

31

I think your parsing worked. The problem is when converting back to string. You can provide the desired format in parameter :

DateTime date = DateTime.ParseExact("2010-01-01 23:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
string formattedDate = date.ToString("yyyy-MM-dd HH:mm:ss")
Console.WriteLine(formattedDate);

By default (without a specified format), it uses formatting information derived from the current culture.

Plue
  • 1,739
  • 1
  • 18
  • 22
  • can you tell the compiler to use the parsed format and not fallback to current culture. – danarj Jan 27 '14 at 14:21
  • @danarj - sure, by using either the overload of `ToString` that takes an `IFormatProvider` or the one that takes a `string`. – D Stanley Jan 27 '14 at 14:25
  • 1
    @danarj - at the point that you have it as a `DateTime` value, then as per my comments, what we have is a structure that counts the number of 100 nanosecond intervals since midnight on 01/01/0001 and what type (Local, UTC or unspecified) it is. It doesn't "remember" how it was created. – Damien_The_Unbeliever Jan 27 '14 at 14:26
  • You can't. Look at Jon Skeet's answer here :http://stackoverflow.com/questions/15407499/set-datetime-format – Plue Jan 27 '14 at 14:26
8

Because 2014-01-01 23:00:00 IS 2014-01-01 11:00:00 PM.

Better explanation

You are implicitly calling DateTime.ToString(), which by default uses the General ("G") format, which in the en-US culture is MM/dd/yyyy hh:mm:ss tt.

If you want to display the time in a different format, you need to specify it:

string s = DateTime.ParseExact("2010-01-01 23:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
Console.WriteLine(s.ToString("yyyy-MM-dd HH:mm:ss");

Or since you're using the same format string, just store it:

string format = "yyyy-MM-dd HH:mm:ss";
DateTime dt = DateTime.ParseExact("2010-01-01 23:00:00", format , CultureInfo.InvariantCulture);
Console.WriteLine(s.ToString(format));
D Stanley
  • 149,601
  • 11
  • 178
  • 240