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

后端 未结 11 1604
既然无缘
既然无缘 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 20:10

    Here is a modified version of the chosen answer:

    getMonth("Feb")
    function getMonth(month) {
      d = new Date().toString().split(" ")
      d[1] = month
      d = new Date(d.join(' ')).getMonth()+1
      if(!isNaN(d)) {
        return d
      }
      return -1;
    }
    

提交回复
热议问题