Reputation: 1733
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
Upvotes: 1
Views: 128
Reputation: 29006
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
Upvotes: 6