简体   繁体   中英

C# DateTime.ParseExact produces wrong dates

I'm trying to parse a date string in the form dd/mm/yyyy but I keep getting a consistent yet invalid results. What am I doing wrong?

here's the code and it's input

DateTime.ParseExact("23/09/2018","d/m/yyyy",CultureInfo.InvariantCulture) => 1/ 23 /18

DateTime.ParseExact("8/10/2018","d/m/yyyy",CultureInfo.InvariantCulture) => 1/ 8 /18

DateTime.ParseExact("30/04/2018","d/m/yyyy",CultureInfo.InvariantCulture) => 1/ 30 /18

I've tried using CultureInfo.GetCultureInfo("en-UK") , CultureInfo.GetCultureInfo("en-GH") but still same results.

I've tried using "dd/mm/yyyy" still same results.

I've also tried qouting the slashed as described here but still same results.

Try this:

DateTime.ParseExact("23/09/2018","dd/MM/yyyy",CultureInfo.InvariantCulture)

MM/M is used to parse months, mm/m is used for minutes.

In C# 'M' is used for month and 'm' used for minute, Here you can find all details.

So please try:

   Console.WriteLine(DateTime.ParseExact("8/10/2018","d/M/yyyy",System.Globalization.CultureInfo.InvariantCulture));

for custom implementation you can visit Here .

小“ m”代表分钟,尝试“ M”或“ MM”几个月

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM