Easiest way to convert month name to month number in JS ? (Jan = 01)

后端 未结 11 1600
既然无缘
既然无缘 2020-11-27 19:21

Just want to covert Jan to 01 (date format)

I can use array() but looking for another way...

Any suggestion?

11条回答
  •  时光取名叫无心
    2020-11-27 19:59

    Here is a simple one liner function

    //ECHMA5
    function GetMonth(anyDate) { 
       return 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'.split(',')[anyDate.getMonth()];
     }
    //
    // ECMA6
    var GetMonth = (anyDate) => 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'.split(',')[anyDate.getMonth()];
    

提交回复
热议问题