Failed to parse format date string, e.g. 2013-09-17T05: 15: 27.947

I am trying to parse the format date:

2013-09-17T05: 15: 27,947

This is my code.

String MessageRecieptDate = messageReceiptDate.Replace("T", " ").Remove(messageReceiptDate.Length-4); DateTime dt = new DateTime(); IFormatProvider culture = new CultureInfo("en-US"); dt = DateTime.ParseExact(MessageRecieptDate, "dd MMM", culture); 

But every time he gives some kind of exception in the format. Something seems to be missing me.

+7
c # datetime parsing
source share
6 answers

What should work:

 //without ParseExact var t1= DateTime.Parse(dt); //you don't know how many milliseconds will be in your string, //and want absolutely use ParseExact anyway var t2= DateTime.ParseExact(dt.PadRight(27, '0'), "o", culture); //you know you'll always have 3 chars for milliseconds. var t3= DateTime.ParseExact(dt, "yyyy-MM-ddTHH:mm:ss.fff", culture); 
+4
source share

I have no idea why you are using "dd MMM" as the format string when your date is "2014-02-03T19:00:00" . There two formats have nothing in common.

The correct format string for your input is "yyyy-MM-ddTHH:mm:ss" :

 string value = "2014-02-03T19:00:00"; DateTime dateValue = DateTime.ParseExact(value, "yyyy-MM-ddTHH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); 
+10
source share

Well, Marcin's answers are perfectly correct, but I want to indicate the root of your problem.

When you write

 string MessageRecieptDate = messageReceiptDate.Replace("T", " ").Remove(messageReceiptDate.Length-4); 

Actually, you delete the line too much. After this line, your line will look like this:

 2014-02-03 19:0 

Try using Remove(messageReceiptDate.Length - 3); instead Remove(messageReceiptDate.Length - 3); . This will make your line 2014-02-03 19:00 exactly the way we want.

Then you should use the yyyy-MM-dd HH:mm format, which exactly corresponds to 2014-02-03 19:00 .

 string messageReceiptDate = "2014-02-03T19:00:00"; string MessageRecieptDate = messageReceiptDate.Replace("T", " ").Remove(messageReceiptDate.Length - 3); IFormatProvider culture = new CultureInfo("en-US"); DateTime dt = DateTime.ParseExact(MessageRecieptDate, "yyyy-MM-dd HH:mm", culture); Console.WriteLine(dt); 

The output will be:

 2/3/2014 7:00:00 PM 

Here is the demo .

+3
source share

The format of your string is completely wrong:

You need to specify the format corresponding to the input line, i.e.: "yyyy-MM-ddTHH:mm:ss"

  string MessageReceiptDate = "2014-02-03T19:00:00"; var datex = DateTime.ParseExact(MessageReceiptDate, "yyyy-MM-ddTHH:mm:ss", culture); 
+2
source share

use DateTime.TryParseExact

 DateTime dateValue; string dateString = "2013-09-17T05:15:27.947"; string[] formats= {"yyyy-MM-ddTHH:mm:ss"}; if(DateTime.TryParseExact(dateString, formats, new CultureInfo("en-US"), DateTimeStyles.None, out dateValue)) { //parsing successful } 
+2
source share

This line:

 dt = DateTime.ParseExact(MessageRecieptDate, "dd MMM", culture); 

has an incorrect date format.

It should be something like:

 dt = DateTime.ParseExact(MessageRecieptDate, "yyyy-MM-ddTHH:mm:ss", culture); 
+1
source share

All Articles