Moment.js没有正确显示所有日期

时间:2015-09-04 00:06:13

标签: javascript date momentjs

我试图使用Moment.js显示下一个即将到来的星期四,并且不确定为什么中间日期正在跳过。例如:代替显示:9月3日,10日,17日,24日,10月1日

它显示9月3日,10日,24日,10月15日,11月12日,12月17日等......

这是我到目前为止所拥有的:

     i need to use the results to compare it to the table values    
     id realtionships   
     1  father
     2  mothera
     31 sisterinlaw
      23    son
    24  daughter

      i have already printed the results of the query like this
      inside a function()
    {
     query = "select id , relationships from table"
      return result
     result= db.rows(query)
     }

     now i need to compare the results of the table  with the response 

2 个答案:

答案 0 :(得分:2)

我相信答案可以在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

可能有更明智的方法可以做到这一点,但我并不熟悉时刻库。

答案 1 :(得分:1)

以下是从本周开始我将如何找到星期四的下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

jsbin:https://jsbin.com/gojetisawu/1/edit?html,output