Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert string to datetime with form yyyy-MM-dd HH:mm:ss in C#

Tags:

c#

datetime

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.

like image 592
danarj Avatar asked Jan 27 '14 14:01

danarj


People also ask

How to convert string to yyyy-mm-dd HH mm ss?

DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); String strDate = dateFormat. format(date); System.

How to convert DateTime in dd mm yyyy HH mm format in c#?

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


1 Answers

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.

like image 169
Plue Avatar answered Sep 30 '22 11:09

Plue