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.
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); String strDate = dateFormat. format(date); System.
You have to use DateTime. ParseExact("7/3/2015 1:52:16 PM", "d/M/yyyy h:mm:ss tt", CultureInfo. InvariantCulture); since the hour can also have a single digit, so "d/M/yyyy h:mm:ss tt" instead of "d/M/yyyy hh:mm:ss tt" .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With