开发者

Date time TryParse issue

开发者 https://www.devze.com 2023-03-23 06:11 出处:网络
I am reading csv file where one of tha columns has date format like tat: Day/Month/Year eg: 30/07/2010

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号