Skip to content

How to parse a date in M/yyyy in C#

February 9, 2015

A little quick tip for parsing the DateFormat of MM/yyyy is to use ParseExact method on “M/yyyy” written below

DateTime dt = new DateTime();             

dt = DateTime.ParseExact(Months[i-1], “M/yyyy”, System.Globalization.CultureInfo.InvariantCulture);

dt = dt.AddMonths(1);

Note that this allows you to make adding months easier when using the method .AddMonths(1) which is why I have used “M/yyyy”. I have noticed that “MM/yyyy” will throw an error if the data being parsed is “6/2015” for example.

Hope that helps!

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: