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.
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.
You can also use ParseExact
method. i.e,
string month = "Dec";
int MonthDigit = DateTime.ParseExact(month, "MMM", CultureInfo.InvariantCulture).Month;
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#?