Moment.js 没有正确显示所有日期
Moment.js not displaying all dates correctly
我正在尝试使用 Moment.js 显示下一个即将到来的星期四,但不确定为什么会跳过中间日期。例如:而不是显示:Sep 3, 10, 17, 24, Oct 1
而是显示 9 月 3 日、10 日、24 日、10 月 15 日、11 月 12 日、12 月 17 日等...
这是我目前的情况:
var date = moment();
var y = 3;
while (y<56) {
var nextThursday = date.weekday(y).add(1,"days").format("dddd, MMM Do");
var addValueThursday = nextThursday;
var thurs = {"text": addValueThursday};
console.log(addValueThursday);
y+=7;
}
我相信可以在 add 函数的作用中找到答案:通过添加时间来改变原始时刻
所以你继续递增一个已经递增(通过突变)的日期。
一个解决方案是在循环内重置日期:
var y = 3;
while (y<56) {
var date = moment();
var nextThursday = date.weekday(y).add(1,"days").format("dddd, MMM Do");
var addValueThursday = nextThursday;
var thurs = {"text": addValueThursday};
console.log(addValueThursday);
y+=7;
}
这给出了输出:
Thursday, Sep 3rd
Thursday, Sep 10th
Thursday, Sep 17th
Thursday, Sep 24th
Thursday, Oct 1st
Thursday, Oct 8th
Thursday, Oct 15th
Thursday, Oct 22nd
可能有更聪明的方法来做到这一点,但我对 moment 库不是很熟悉。
以下是我如何查找从本周开始的星期四的下 10 次出现:
var thursday = moment().startOf('week').add(4, 'days');
for (var i=0; i<10; i++) {
console.log(thursday.format("dddd, MMM Do"));
thursday = thursday.add(1, 'week');
}
这会 console.log
类似于:
Thursday, Sep 3rd
Thursday, Sep 10th
Thursday, Sep 17th
Thursday, Sep 24th
Thursday, Oct 1st
Thursday, Oct 8th
Thursday, Oct 15th
Thursday, Oct 22nd
Thursday, Oct 29th
Thursday, Nov 5th
我正在尝试使用 Moment.js 显示下一个即将到来的星期四,但不确定为什么会跳过中间日期。例如:而不是显示:Sep 3, 10, 17, 24, Oct 1
而是显示 9 月 3 日、10 日、24 日、10 月 15 日、11 月 12 日、12 月 17 日等...
这是我目前的情况:
var date = moment();
var y = 3;
while (y<56) {
var nextThursday = date.weekday(y).add(1,"days").format("dddd, MMM Do");
var addValueThursday = nextThursday;
var thurs = {"text": addValueThursday};
console.log(addValueThursday);
y+=7;
}
我相信可以在 add 函数的作用中找到答案:通过添加时间来改变原始时刻
所以你继续递增一个已经递增(通过突变)的日期。
一个解决方案是在循环内重置日期:
var y = 3;
while (y<56) {
var date = moment();
var nextThursday = date.weekday(y).add(1,"days").format("dddd, MMM Do");
var addValueThursday = nextThursday;
var thurs = {"text": addValueThursday};
console.log(addValueThursday);
y+=7;
}
这给出了输出:
Thursday, Sep 3rd
Thursday, Sep 10th
Thursday, Sep 17th
Thursday, Sep 24th
Thursday, Oct 1st
Thursday, Oct 8th
Thursday, Oct 15th
Thursday, Oct 22nd
可能有更聪明的方法来做到这一点,但我对 moment 库不是很熟悉。
以下是我如何查找从本周开始的星期四的下 10 次出现:
var thursday = moment().startOf('week').add(4, 'days');
for (var i=0; i<10; i++) {
console.log(thursday.format("dddd, MMM Do"));
thursday = thursday.add(1, 'week');
}
这会 console.log
类似于:
Thursday, Sep 3rd
Thursday, Sep 10th
Thursday, Sep 17th
Thursday, Sep 24th
Thursday, Oct 1st
Thursday, Oct 8th
Thursday, Oct 15th
Thursday, Oct 22nd
Thursday, Oct 29th
Thursday, Nov 5th