How to find dynamically next month and year using moment js in jQuery

Wolverine picture Wolverine · Nov 7, 2014 · Viewed 30.4k times · Source

I have two calenders in my page which is implemented using full calender jQuery plugin.

I need to load calenders like left present calender and right upcoming month calender.Like if present is November then right calender should be december. Which i was able to do so by giving dates.

But i have two navigation arrows to navigate the calender at both left and right to these calenders. How can i get dynamic next month and year and vice-versa ie prev month and prev year provided a month and year.

I found a way to get current current month and year using moment.js which will handle leap year and all...

Below is the code so far to get current date. I need to know how can i get prev month and year and next month and year on navigation clicks.

Code:

var eNow = new Date();
var eMoment = moment(eNow);

var leftEMonth = eMoment.format('MM')-1; 
var leftEYear = eMoment.format('YYYY');

Answer

meskobalazs picture meskobalazs · Nov 7, 2014

Months:

moment().add(1, 'months');

Years:

moment().add(1, 'years');

So for example your next month button would look like this:

$('#nextMonthBtn').click(function () {
    eMoment.add(1, 'months');
});

assuming eMoment is in scope of course.