Javascript 如何在jQuery中使用moment js动态查找下个月和下一年
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26800749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find dynamically next month and year using moment js in jQuery
提问by Wolverine
I have two calenders in my page which is implemented using full calender jQuery plugin.
我的页面中有两个日历,它们是使用完整的日历 jQuery 插件实现的。
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.
我需要加载日历,如左侧当前日历和右侧即将到来的月份日历。如果现在是 11 月,那么右侧日历应该是 12 月。我可以通过给出日期来做到这一点。
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...
我找到了一种使用 moment.js 获取当前月份和年份的方法,它可以处理闰年和所有...
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');
回答by meskobalazs
Months:
月份:
moment().add(1, 'months');
Years:
年:
moment().add(1, 'years');
So for example your next monthbutton would look like this:
例如,您的下个月按钮将如下所示:
$('#nextMonthBtn').click(function () {
eMoment.add(1, 'months');
});
assuming eMoment is in scope of course.
假设 eMoment 在当然的范围内。

