Firebird date cannot be parsed
Question
I have a firebird datebase, some tables have timestamp. For example, my database returns such dates: "1/4/2012 3:08:44 PM" or "12/20/2011 4:38:02 PM". I use TryParseExact and code is as follows:
DateTime.TryParseExact(results[i][1], "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt)
I tried many formats like "MM/dd/yyyy hh:mm:ss tt" but none of them worked. Please help me or I will go crazy this time...
Solution
Have a look at this article
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx
The key part to look at is
// Parse date-only value without leading zero in month using "d" format.
// Should throw a FormatException because standard short date pattern of
// invariant culture requires two-digit month.
dateString = "6/15/2008";
try {
result = DateTime.ParseExact(dateString, format, provider);
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException) {
Console.WriteLine("{0} is not in the correct format.", dateString);
}
So it may be that your database is not returning a standard pattern. Seems silly to me that this would throw an exception though. And in the example it is only saying a "date-only" value, which yours is not.
If you have the date in a string you could do the old manual fix-up:
public static string FixDate(string date)
{
return (date.IndexOf('/') == 1) ? "0" + date : date;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow