View Full Version : DateTime.ParseExact Problem
11-22-2006, 04:00 PM
Can someone please tell me what I'm doing wrong:
I have this string: 11/24/2006 03:30
That I am trying to put in the following format: 2006-11-24 03:30 AM
I tried using Parse Exact:
datetime.parseExact('11-24-2006 03:30','yyyy-MM-dd HH:mm tt*',CultureInfo)
That generates the following error:
System.FormatException: String was not recognized as a valid DateTime.
Can someone please tell me what I am doing wrong. Is the string format wrong?
11-22-2006, 04:18 PM
ParseExact means the string is already IN that format and you want a date out of it. If you want to convert a string that is in one format into a different format, you make a date first, then format that date.
string origDateString = "11-24-2006 03:30";
string desiredFormat = "yyyy-MM-dd HH:mm tt*";
DateTime datetime = DateTime.Parse(origDateString);
string convertedDateString = datetime.toString(desiredFormat);
You could shorten that up more and pass culture arguments etc. if you had a mind to, but you get the idea. I also generally use TryParse when playing with date parsing, but that's a 2.0 thing so it won't work with 1.1.
11-22-2006, 05:03 PM
I think I'm going in the right direction at least now. The only problem now is that the date is coming back like this: 0000-00-00 00:00. Eventhough, the original string is 2006-11-24 23:59.
11-22-2006, 05:07 PM
Can you post the code?
11-22-2006, 05:37 PM
I figured out the problem. It wasn't really with the code, there was a problem in Mysql once I tried inserting the value. The format had to be in yyyy-mm-dd H:mm (24 hour clock) and there was a space between the time and the AM and PM (which really didn't matter since MySql stripped the AM/PM anyway) which was causing the initial error.
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.