问题
I have a daterangepicker function that is returning the selected date in this format 06 May 2016. What I am trying to do is extract the month as an integer, therefore from the above I should be able to return the number 5.
This is the line of code that returns the selected date - getDateString(new Date(opt.start)
Any help is appreciated thanks.
回答1:
var date = new Date();
var month = date.getMonth();
The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.
回答2:
var datestring = getDateString(new Date(opt.start);
var monthNumber = new Date(datestring).getMonth()+1;
回答3:
I'm not sure this will help since I am new in this field
var today = new Date();
var month = new Array();
month[0] = "01";
month[1] = "02";
month[2] = "03";
month[3] = "04";
month[4] = "05";
month[5] = "06";
month[6] = "07";
month[7] = "08";
month[8] = "09";
month[9] = "10";
month[10] = "11";
month[11] = "12";
var monthNumber = month[today.getMonth()];
console.log("This month is " + monthNumber);
来源:https://stackoverflow.com/questions/37070273/get-month-number-from-a-date-in-javascript