I found a lot of ways to get the name from a number but now I need it the other way around.
If the string equals April, I want to convert it to int "4".
Does anyone know the best way to accomplish this server side?
I found a lot of ways to get the name from a number but now I need it the other way around.
If the string equals April, I want to convert it to int "4".
Does anyone know the best way to accomplish this server side?
int month = DateTime.ParseExact(MonthNameStr, "MMMM", CultureInfo.CurrentCulture ).Month
or you can do
int month = DateTimeFormatInfo.CurrentInfo.MonthNames.ToList().IndexOf(MonthNameStr) + 1;
int month = DateTime.ParseExact("April", "MMMM", CultureInfo.InvariantCulture).Month;
You can use the DateTime.ParseExact Method with a custom format specifier consisting only of the the "MMMM" Custom Format Specifier:
int month = DateTime.ParseExact("April", "MMMM", new CultureInfo("en-US"))
.Month;