Is there a function in C# that can already change a Month name to it's corresponding month number? If not, should I make a method (like using 'Switch" or some loop function) that makes this possible?
I'm asking because I would like to have clean code and not make a huge mess in my code. Thanks in advance
DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture).Month
You can use :
Convert.ToDateTime(monthName + " 01, 1900").Month;
or
Array.IndexOf(DateTimeFormatInfo.CurrentInfo.MonthNames,
monthName.ToLower(CultureInfo.CurrentCulture)) + 1;
and also
Array.FindIndex(DateTimeFormatInfo.CurrentInfo.MonthNames,
m => m.Equals(monthName, StringComparison.OrdinalIgnoreCase)) + 1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With