如何使用JavaScript查找下一个月和上一个月?

如何使用JavaScript查找下一个月和上一个月?,javascript,jquery,date,monthcalendar,Javascript,Jquery,Date,Monthcalendar,我的jQuery函数接受当前月份的。我想根据点击的按钮显示下一个月和上一个月 我的问题是,我是否可以调用defaultDate()函数来了解当前月份的下一个月和上一个月 $(document).ready(function () { var current_date = $('#cal-current-month').html(); //current_date will have September 2013 $('#previous-month').onclick(f

我的jQuery函数接受当前月份的
。我想根据点击的按钮显示下一个月和上一个月

我的问题是,我是否可以调用
default
Date()
函数来了解当前月份的下一个月和上一个月

$(document).ready(function () {
    var current_date = $('#cal-current-month').html();
    //current_date will have September 2013
    $('#previous-month').onclick(function(){
        // Do something to get the previous month
    });
    $('#next-month').onclick(function(){
        // Do something to get the previous month
    });
});
我可以编写一些代码并获得下一个月和上一个月的数据,但我想知道是否有任何已定义的函数用于此目的

已解决

var current_date = $('.now').html();
var now = new Date(current_date);

var months = new Array( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

$('#previous-month').click(function(){
    var past = now.setMonth(now.getMonth() -1);
    $('.now').html(months[now.getMonth()]+' '+now.getFullYear());
});

$('#next-month').click(function(){
    var future = now.setMonth(now.getMonth() +1);
    $('.now').html(months[now.getMonth()]+' '+now.getFullYear());
});

如果你只想得到下个月的第一天,你可以做如下事情:

var now = new Date();
var future = now.setMonth(now.getMonth() + 1, 1);
var past = now.setMonth(now.getMonth() - 1, 1);
这将防止“下一个”月跳过一个月(例如,如果省略第二个参数,则将一个月添加到2014年1月31日将导致2014年3月3日)

另外,使用*可以执行以下操作:

var today = Date.today();
var past = Date.today().add(-1).months();
var future = Date.today().add(1).months();
在本例中,我使用今天的日期,但它适用于任何日期


*date.js已被放弃。如果您决定使用库,您可能应该按照RGraham的建议使用矩.js。

这很有帮助。非常感谢:)我设置了-
new Date().setMonth(new Date().getMonth()+2,1)
我得到了-
1404212502044
。该数字是多少以及如何提取和显示月份?@arjun
const month_name=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”];月份名称[新日期(未来).getMonth()]