Get month number from a date in javascript

妖精的绣舞 提交于 2021-01-28 16:39:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!