Reputation: 211
Any idea why when i have time "00:00:00" in my string this does't work ?
DateTime.TryParseExact("01/06/2015 00:00:00", "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out tempDate)
While when i'm doing this it works Ok:
DateTime.TryParseExact("01/06/2015", "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out tempDate)
Any help will be much appreciated! Thanks
Upvotes: 2
Views: 1599
Reputation: 1904
Unfortunately, I tried the answer and it didn't work.
It turns out that there was a space character at the beginning of the date string I was trying to ParseExact. (e.g. " 20/08/2019")
So try a close examination of your date string and make sure there's no hidden characters or space characters you didn't spot
Upvotes: 0
Reputation: 98810
From documentation;
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly.
For your second example they match, but in first example, they don't.
Use dd/MM/yyyy HH:mm:ss
format instead.
string s = "01/06/2015 00:00:00";
DateTime dt;
if(DateTime.TryParseExact(s, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
// Successfully parsing
}
More information at;
Upvotes: 5