user3055923
user3055923

Reputation: 195

String Date to DateTime Format

I have a string with a value of 13/12/17,09:37:20+32 I cant convert it in datetime format. Error always occur saying that the "String was not recognized as a valid DateTime". This is my code:

 DateTime crtdDate = DateTime.ParseExact(l.CreateDate, "yy/MM/dd,hh:mm:ss tt", CultureInfo.InvariantCulture);

Please someone help me.Thanks!

Upvotes: 0

Views: 292

Answers (2)

Rickey
Rickey

Reputation: 7880

Your string must match the format exactly:

 l.CreateDate = "06/15/2008";

 [http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx][1]
 "d" -> 6/15/2009 1:45:30 PM -> 6/15/2009 (en-US)
        6/15/2009 1:45:30 PM -> 15/06/2009 (fr-FR)
        6/15/2009 1:45:30 PM -> 2009/06/15 (ja-JP)



 DateTime.ParseExact(l.CreateDate, "d", CultureInfo.InvariantCulture);

Without the +32, this will parse:

 DateTime.ParseExact("13/12/17,09:37:20", "yy/MM/dd,hh:mm:ss", CultureInfo.InvariantCulture);

Use a valid timezone specifier, the following will work:

 DateTime crtdDate = DateTime.ParseExact("13/12/17,09:37:20+00:00", "yy/MM/dd,hh:mm:ssK", 
            CultureInfo.InvariantCulture);

Upvotes: 0

sharoz
sharoz

Reputation: 6345

tt represents either am or pm

+32 won't be parsed by tt. If you are trying to parse hundreths of a second, try ff.

var  crtdDate = DateTime.ParseExact(l.CreateDate, "yy/MM/dd,hh:mm:ss+ff", CultureInfo.InvariantCulture);

Upvotes: 1

Related Questions