In the C#, I am parsing a date from string, but it gives me error
DateTime.Parse("07/26/2012");
the error
System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
at System.DateTime.Parse(String s)
is it related to date format ? is it related to my pc setting ?
Thanks
By default, Parse
uses your current culture. ParseExact
allows you to manually specify the date format.
Try this instead:
DateTime date = DateTime.ParseExact("07/26/2012", "MM/dd/yyyy", CultureInfo.InvariantCulture);
The InvariantCulture
option allows you to ignore the current culture settings on the system.
Maybe the culture in which you are running this is not compatible with this date format. You could use InvariantCulture
:
DateTime.Parse("07/26/2012", CultureInfo.InvariantCulture);
Remember that the Parse
method uses the current thread culture.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With