简体   繁体   中英

my datetime string is not recognized as valid DateTime format

I've completely stumped on this one. Trying to convert my user input string datetime (format dd-mm-yyyy) into a DateTime object, but unable to convert and keep getting above error.

if I try to use:

DateTime dt = DateTime.ParseExact(dob, "dd-MMM-YYYY", CultureInfo.InvariantCulture);

where dob is string input from user in dd-mm-yyyy format.

It won't give error although conversion is different.

For eg;

Input: 22-10-1984 Output: 1-22-1984

You can alternatively use the Convert.ToDateTime method if ParseExact method isn't suitable for you. Just check here for more information.

Use proper datetime format.

MM- month. yyyy- years.

DateTime dt = DateTime.ParseExact(dob, "dd-MM-yyyy", CultureInfo.InvariantCulture);

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