Converting month name to month number using JavaScript

Is there a a better solution than what I have below to convert a month name to a month number using JavaScript or PHP?

I did like this:

$monNum =  date('n',strtotime($staff->curMonth));

Answers


You could keep an object of key/value pairs where the key is the month name and the value is the month number:

var months = {
    January: 1,
    February: 2,
    ...
};

and then:

var monthNumber = months['April'];

or:

var monthNumber = months.April;

Need Your Help

Increment counter within ng-repeat in AngularJS

javascript angularjs

I have two nested ng-repeat and would like to access both the outer and inner indexes.