Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a string containing monthName to Int of MonthDigit

I have a string which has short month name in it.\

string month = "Jun";

I need to get month in digit from this month name.

Say i do this:

int monthInDigit = getMonth(month);

monthInDigit <-- 6

How can i achieve this. If you cant get my question pleases comment i will explain it proprly.

Thanxx in advance

like image 651
Murtaza Munshi Avatar asked Oct 07 '13 08:10

Murtaza Munshi


2 Answers

int monthInDigit = DateTime.ParseExact(month, "MMM", CultureInfo.InvariantCulture).Month;
like image 106
Arsen Mkrtchyan Avatar answered Jan 01 '23 12:01

Arsen Mkrtchyan


You can parse it to a DateTime first:

DateTime dt = DateTime.ParseExact("Jun", "MMM", CultureInfo.InvariantCulture);
int month = dt.Month;
if(month < 6)
{  
    // do something...
}
like image 34
Tim Schmelter Avatar answered Jan 01 '23 11:01

Tim Schmelter