1

I have the following string "Dec".

How can I convert this string to the month number 12?

I tried this:

DateTime.ParseExact("Dec", "MMMM", null).Month

This doesn't work.

Andreas
  • 2,252
  • 1
  • 19
  • 29
user3702149
  • 17
  • 1
  • 6

2 Answers2

3

You can also use ParseExact method. i.e,

string month = "Dec";
int MonthDigit = DateTime.ParseExact(month, "MMM", CultureInfo.InvariantCulture).Month;
Zeeshan
  • 2,884
  • 3
  • 28
  • 47
2

Simply use "MMM":

DateTime.ParseExact("Dec", "MMM", null).Month

also best use a specific culture, so its consisten with the input:

DateTime.ParseExact("Dec", "MMM", System.Globalization.CultureInfo("en-us")).Month

The long version MMMM refers to a full month name as originaly posted here How to parse a month name (string) to an integer for comparison in C#?

Community
  • 1
  • 1
maximbr
  • 401
  • 6
  • 7