I need to be able to compare some month names I have in an array.
It would be nice if there were some direct way like:
Month.toInt("January") > Month.toInt("May")
My Google searching seems to suggest the only way is to write your own method, but this seems like a common enough problem that I would think it would have been already implemented in .Net, anyone done this before?
You don't have to create a DateTime instance to do this. It's as simple as this:
I'm running on the
da-DK
culture, so this unit test passes:I'll leave it as an exercise to the reader to create an overload where you can pass in an explicit CultureInfo.
One simply solution would be create a Dictionary with names and values. Then using Contains() you can find the right value.
I translate it into C# code in Spanish version, regards:
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.caution code is in Beta version.
You could do something like this: