Javascript Moment.js添加到日期(每月两次)

时间:2016-09-22 21:48:26

标签: javascript date momentjs

我正在使用Moment.js,我需要创建一个for循环,将一个特定天数添加到名为theDate的日期变量中,以便'theDate'以半月为单位继续推进(每月两次)。

此示例适用于将一天(逐个)添加到dueDate

for (var i = 1; i <= 10; i++) {
    moment(theDate).add(i, 'day').add(1,'day');
    console.log(dueDate);
}

如果'theDate'设置为18.Jan.2017,则上面会打印以下内容:

1/18/17
1/19/17
1/20/17
1/21/17
1/22/17

...

我需要弄清楚如何添加天数(或周数等),以便结果是在半月(每月两次)订单中添加天数。这意味着,如果我的开始日期设置为18.Jan.2017,则结果在整个2017期间应如下所示:

1/18/17
2/01/17
2/18/17
3/04/17
3/18/17
4/01/17
4/18/17
5/02/17
5/18/17
6/01/17
6/18/17
7/02/17
7/18/17
8/01/17
8/18/17
9/01/17
9/18/17
10/02/17
10/18/17
11/01/17
11/18/17
12/02/17
12/18/17

请注意,我不是在寻找第1和第15类型的解决方案,因为我们通常会看到薪水。我需要每月两次根据开始日期付款,这可以是用户决定将其设置为的任何内容。

3 个答案:

答案 0 :(得分:1)

编辑根据您的回答,我调整了代码以进行优化,使其更加清晰,并使其开箱即用。

for (var i = 0; i < 24; i++) {
    var finalDate = moment([2017, 0, 31]).add(Math.ceil(0.5 * i), 'month');
    if (i % 2 !== 0) {
        finalDate.subtract(2, 'weeks');
    }
    console.log(finalDate.format("YYYY/MM/DD"));
}

这完全输出:

2017/01/31
2017/02/14
2017/02/28
2017/03/17
2017/03/31
2017/04/16
2017/04/30
2017/05/17
2017/05/31
2017/06/16
2017/06/30
2017/07/17
2017/07/31
2017/08/17
2017/08/31
2017/09/16
2017/09/30
2017/10/17
2017/10/31
2017/11/16
2017/11/30
2017/12/17
2017/12/31
2018/01/17

请注意添加半个月的时间会增加整整一个月。为了使这种行为更加明显,我明确地进行了整理。

基于假设的原始答案

如果您希望半月访问用户的眼睛,您可以选择用户选择的那一天,并根据它自己设置第二个日期。

如果用户选择第23天,则添加15,如果超过28,则包装(确定)并将第二个日期设置为每月的第10天。

由于每个月都是两个固定日期,因此您不再需要添加天数。你只需要添加一个月。就这么简单:

var userChosenDate = 23,
    // if the user chose 28 and higher, just use 28 as a starting point.
    yourChosenPaymentDate = (Math.min(userChosenDate, 28) + 15) % 28,
    firstPayment = Math.min(userChosenDate, yourChosenPaymentDate),
    secondPayment = Math.max(userChosenDate, yourChosenPaymentDate);

for (var i = 1; i <= 12; i++) {
  console.log(i + "/" + firstPayment + "/2017");
  console.log(i + "/" + secondPayment + "/2017");
}

使用momentJs:

var userChosenDate = 23,
    yourChosenPaymentDate = (Math.min(userChosenDate, 28) + 15) % 28,
    firstPayment = moment(Math.min(userChosenDate, yourChosenPaymentDate) + "/1/2017", "D/M/YYYY"),
    secondPayment = moment(firstPayment)
                      .date(Math.max(userChosenDate, yourChosenPaymentDate)),
    format = "MM/DD/YYYY";


console.log(firstPayment.format(format));
console.log(secondPayment.format(format));
for (var i = 1; i < 12; i++) {
    console.log(firstPayment.add(1, 'month').format(format));
    console.log(secondPayment.add(1, 'month').format(format));
}

答案 1 :(得分:0)

我对此感到疑惑,直到我发现自己找到了最适合我的答案:

for (var i = 1; i <= 24; i++) {
   if (i % 2 === 1) {
     finalDate = moment(theDate).add((1/2)*i, 'month');
   } else {
     finalDate = moment(theDate).add((1/2)*i, 'month').subtract(2,'weeks');
   }
  console.log(finalDate);
}

所以,例如如果theDate设置为2017年1月18日,它将打印:

January 18th 2017
February 3rd 2017
February 18th 2017
March 3rd 2017
March 18th 2017
April 3rd 2017
April 18th 2017
May 3rd 2017
May 18th 2017
June 3rd 2017
June 18th 2017
July 3rd 2017
July 18th 2017
August 3rd 2017
August 18th 2017
September 3rd 2017
September 18th 2017
October 3rd 2017
October 18th 2017
November 3rd 2017
November 18th 2017
December 3rd 2017
December 18th 2017

答案 2 :(得分:-1)

试试我的clock.js脚本。

&#13;
&#13;
var twoWeeks = clock.unit.weeks * 2;
var twoWeeksFromToday = clock.now + twoWeeks;

console.log(clock.what.weekday(twoWeeksFromToday) + ' ' + clock.what.month(twoWeeksFromToday) + ' ' + clock.what.day(twoWeeksFromToday));
&#13;
<script src="https://rack.pub/clock.min.js"></script>
&#13;
&#13;
&#13;