最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

daterangepicker - Get month number from a date in javascript - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question asked May 6, 2016 at 10:38 Bob the BuilderBob the Builder 5433 gold badges12 silver badges35 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 13
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.

var datestring = getDateString(new Date(opt.start);
var monthNumber = new Date(datestring).getMonth()+1;

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);

Thanks, and more simple:

var month = new Date().getMonth() + 1;

// Result, ie: January = 1 ... December = 12

var month = new Date().getMonth() + 1;
console.log("month", month);

发布评论

评论列表(0)

  1. 暂无评论