Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert dd/MM/yyyy hh:mm:ss.fff from String to DateTime in C#

I am trying to convert a string of the below format dd/MM/yyyy hh:mm:ss.fff into DateTime value

easiest way?

BTW

IFormatProvider culture = new CultureInfo("en-US", true);
DateTime.ParseExact(timeString, "dd/MM/yyyy hh:mm:ss.fff",culture);

Throws an invalid time exception

Eg. 11/12/2009 13:30:00.000 Where 12 is the month (i know weird)

like image 927
soldieraman Avatar asked Dec 23 '09 06:12

soldieraman


2 Answers

You have to use HH

string timeString = "11/12/2009 13:30:00.000";
IFormatProvider culture = new CultureInfo("en-US", true); 
DateTime dateVal = DateTime.ParseExact(timeString, "dd/MM/yyyy HH:mm:ss.fff", culture);
like image 70
Adriaan Stander Avatar answered Sep 22 '22 07:09

Adriaan Stander


hh:mm:ss.fff should be HH:mm:ss.fff since you're using 24-hour format.

like image 44
Anton Gogolev Avatar answered Sep 21 '22 07:09

Anton Gogolev