I am reading csv file where one of tha columns has date format like tat: Day/Month/Year
eg: 30/07/2010
But when i am using DateTime.TryParse()
to parse that sting in to datetinme method TryParse()
treated first numbers like month (number 30 in example above), so i am getting incorrect date exception.
How may i say to Datetime.TryParse()
that first numbers in string is day and not a month?
UPDATE:
Why if i changed date to Mont开发者_高级运维h/Day/Year
eg: 7/30/2010
this not working:
DateTime.TryParseExact("7/30/2010", "m/dd/yyyy", null, DateTimeStyles.None, out date);
Any thoughts?
Have a look into using a custom date and time format string.
Also, to use a custom format string, you need to use TryParseExact, ala:
DateTime dt;
DateTime.TryParseExact(dateTime,
"dd/MM/yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dt);
Use the DateTime.TryParseExact method
DateTime dateValue;
var dateString = "30/07/2010";
DateTime.TryParseExact(dateString, "dd/MM/yyyy", new CultureInfo("en-US"), DateTimeStyles.None, out dateValue);
Try:
DateTime.ParseExact(string, "dd/MM/yyyy", null);
Try using TryParseExact()
and pass the format for your date
DateTime.TryParseExact("30/07/2010", "dd/MM/yyyy", null, DateTimeStyles.None, out result)
精彩评论