简体   繁体   中英

String was not recognized as a valid DateTime

I have a string 2016. I am wondering how can I parse this string to a date time? I have tried Datetime.ParseExact but it said it is not a valid format

string period = "2016"
DateTime date = DateTime.ParseExact(period, "YYYY", CultureInfo.InvariantCulture);

Help will be appreciated. Thanks

use "yyyy" instead for YYYY then you will get the expected answer;

string period = "2016";
DateTime date = DateTime.ParseExact(period, "yyyy", CultureInfo.InvariantCulture);

You can check More DateTime Formats here

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