How to parse a month name (string) to an integer for comparison in C#?
You could do something like this:
Convert.ToDate(month + " 01, 1900").Month
DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture ).Month
Although, for your purposes, you'll probably be better off just creating a Dictionary<string, int>
mapping the month's name to its value.
If you use the DateTime.ParseExact()
-method that several people have suggested, you should carefully consider what you want to happen when the application runs in a non-English environment!
In Denmark, which of ParseExact("Januar", ...)
and ParseExact("January", ...)
should work and which should fail?
That will be the difference between CultureInfo.CurrentCulture
and CultureInfo.InvariantCulture
.