0

I have a string like this (in 12 hr clock) in my XML config data file:

expires="10/27/2014 2:42:57 PM"

I want to assign that value as a (datetime) member of a class like this:

 Common.SOExpiries = 
     DateTime.ParseExact(gSet.Attribute("expires").Value, "MM/dd/yyyy hh:mm:ss",    
     CultureInfo.InvariantCulture);

but I am getting the error 'String was not recognized as a valid DateTime'

what am I doing wrong here?

This is in the UK btw, so the CultureInfo should be ok

Our Man in Bananas
  • 5,809
  • 21
  • 91
  • 148
  • 1
    " PM" is not understood by "hh:mm:ss" use "hh:mm:ss tt" – Xaruth Nov 04 '14 at 14:33
  • why in gods green earth someone down graded the question!?!? – Liran Nov 04 '14 at 14:51
  • I'm guessing either it's my repuation that precededs me, or (more likely) it could be that it looks like I didn't do enough research to find out what was wrong...this isn't the case, I did the research, but didn't get the part about the 12 hours clock! – Our Man in Bananas Nov 04 '14 at 14:54

2 Answers2

5

It should be:

"MM/dd/yyyy h:mm:ss tt"

as you forget about ttto indicate AM/PM, and h because you have 1-12 hours, not 01-12 hours format. Consider if your seconds and minutes should be also s and m.

Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58
  • it still throws the same error even if I do this: `Convert.ToDateTime(DateTime.ParseExact(gSet.Attribute("expires").Value, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture));` – Our Man in Bananas Nov 04 '14 at 14:36
1

You need to parse the AM / PM portion as well.

Common.SOExpiries = 
 DateTime.ParseExact(gSet.Attribute("expires").Value, "MM/dd/yyyy hh:mm:ss tt",    
 CultureInfo.InvariantCulture);
Brian Dishaw
  • 5,767
  • 34
  • 49