Vuk Stanojevic

Vuk Stanojevic

  • 1.6k
  • 140
  • 45.6k

C# Convert month name to month number

May 2 2020 9:19 AM
Hi everyone,
i have combobox which i list of all 12 months.
I display there name of each month.
But for the code i must convert it to number.
When i try to convert it i get error "String was not recognized as valid  DateTime".
 
Here is the code:
 
 
//Month name , for example January
string month = comboBox3.Text.Trim();
//Month number according to selected month in combobox3
int month1 = DateTime.ParseExact(month, "MMM",CultureInfo.CreateSpecificCulture("en-GB")).Month;
 
Can you help me?
Thank you.
 

Answers (6)