View Light

Javascript: return month name from month number

// Returns the month name corresponding to the monthNum passed.
// Returns full month name if abbreviate = false.
// Returns three letter abbreviation of month if abbreviate = true.

function NumToMonth(monthNum, abbreviate) {
    switch (monthNum)
    {
        case 1: if (!abbreviate)
                        return "January";
                    else
                        return "Jan";
                     break;
        case 2: if (!abbreviate)
                        return "February";
                    else
                        return "Feb";
                    break;
        case 3: if (!abbreviate)
                        return "March";
                    else
                        return "Mar";
                    break;
        case 4: if (!abbreviate)
                        return "April";
                    else
                        return "Apr";
                    break;
         case 5: return "May";
                    break;
         case 6: if (!abbreviate)
                        return "June";
                    else
                        return "Jun";
                    break;
         case 7: if (!abbreviate)
                        return "July";
                    else
                        return "Jul";
                    break;
         case 8: if (!abbreviate)
                        return "August";
                    else
                        return "Aug";
                     break;
         case 9: if (!abbreviate)
                        return "September";
                    else
                        return "Sep"; // SEP field??
                     break;
        case 10: if (!abbreviate)
                        return "October";
                      else
                        return "Oct";
                      break;
        case 11: if (!abbreviate)
                        return "November";
                      else
                        return "Nov";
                      break;
        case 12: if (!abbreviate)
                        return "December";
                     else
                        return "Dec";
                      break;
         default:
                return "Unknown monthNum " + monthNum;
                break;
    }
}

Rating: (You must be logged in to vote)
Reply