2

私のjQuery関数はcurrent month. クリックしたボタンに応じて、翌月と前月を表示したいと思います。

私の質問は、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
    });
});

defined functionsいくつかのコードを記述して、翌月と前月を取得することはできますが、この目的のために既に何かがあるかどうか疑問に思っていましたか?

解決した

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());
});
4

1 に答える 1