RedRocket
RedRocket

Reputation: 1733

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

Upvotes: 1

Views: 128

Answers (1)

sujith karivelil
sujith karivelil

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

Related Questions