views:

120

answers:

6

I start with c# syntax to get month name from month number,

System.Globalization.DateTimeFormatInfo mfi = new 
                                      System.Globalization.DateTimeFormatInfo();
        string strMonthName = mfi.GetMonthName(8);

and for abbrevated month name i use

        string strMonthName = mfi.GetAbbreviatedMonthName(8)

and to get month number from month name,

public string GetMonthNumberFromAbbreviation(string mmm)
{
  string[] monthAbbrev=CultureInfo.CurrentCulture.DateTimeFormat
                                                .AbbreviatedMonthNames;
  int index = Array.IndexOf(monthAbbrev, mmm) + 1;
  return index.ToString("0#");
}

Now i would like to get the Codes for other programming languages.

+1  A: 

A method to get a month number should return a number.

public int GetMonthNumberFromAbbreviation(string m) {
  return Array.IndexOf(
    CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames,
    m) + 1;
}
Guffa
A: 

In PHP:

function GetMonthNumberFromAbbreviation($s) {
        $d = date_parse($s);
        return $d['month'];
}
codaddict
At least in the question, leading zeros are expected.
eyelidlessness
+1  A: 

In Java (to get month name from number):

String getMonthNameFromNumber(int n){
    DateFormatSymbols DFS = new DateFormatSymbols();
    String[] MonthNames = DFS.getMonths();
    return MonthNames[n-1];
}

Use getShortMonths() instead of getMonths() to get Month names in short format.

Sagar V
+2  A: 

PHP:

Abbreviation to number (leading zero):

function a($s){return date('m',strtotime($s));}

Number to abbreviation:

function b($s){return date('M',strtotime("$s/1"));}
eyelidlessness
+1  A: 

In C

Name from number:

struct tm n;
char s[10];
strftime(s, sizeof(s), "%B", &n);

Getting the number from the name would be a bit clumsy -- there's no real attempt at providing that capability.

Jerry Coffin
A: 

In Mathematica

monthNameFromNumber[x_] := DateString[{0, x}, {"MonthName"}];  
monthNumberFromName[x_] := DateList[{x, {"MonthName"}}][[2]];

Samples:

monthNameFromNumber[4]
Out> April

monthNumberFromName["april"]
Out> 4

monthNumberFromName["Apr"]
Out> 4
belisarius