在 NodeJS 中迭代一系列日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17163809/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:49:08  来源:igfitidea点击:

Iterate through a range of dates in NodeJS

node.jsdatedatetime

提问by nwaltham

I would like to iterate through a range of calender dates, each iteration is +1 day. I would use something built around JodaTime in Java - is there something similar in NodeJS?

我想遍历一系列日历日期,每次迭代为 +1 天。我会在 Java 中使用围绕 JodaTime 构建的东西 - NodeJS 中是否有类似的东西?

回答by Matt Johnson-Pint

You can use moment.jsin a node.js application.

您可以在 node.js 应用程序中使用moment.js

npm install moment

Then you can very easily do this:

然后你可以很容易地做到这一点:

var moment = require('moment');

var a = moment('2013-01-01');
var b = moment('2013-06-01');

// If you want an exclusive end date (half-open interval)
for (var m = moment(a); m.isBefore(b); m.add(1, 'days')) {
  console.log(m.format('YYYY-MM-DD'));
}

// If you want an inclusive end date (fully-closed interval)
for (var m = moment(a); m.diff(b, 'days') <= 0; m.add(1, 'days')) {
  console.log(m.format('YYYY-MM-DD'));
}

Hmmm... this looks a lot like the code you already wrote in your own answer. Moment.js is a more popular library has tons of features, but I wonder which one performs better? Perhaps you can test and let us know. :)

嗯...这看起来很像您已经在自己的答案中编写的代码。Moment.js 是一个更受欢迎的库,有很多功能,但我想知道哪个性能更好?也许您可以测试并告诉我们。:)

But neither of these do as much as JodaTime. For that, you need a library that implements the TZDB in JavaScript. I list some of those here.

但这些都没有 JodaTime 多。为此,您需要一个在 JavaScript 中实现 TZDB 的库。我在这里列出了其中的一些

Also, watch out for problems with JavaScript datesin general. This affects NodeJS as well.

此外,请注意JavaScript 日期的一般问题。这也会影响 NodeJS。

回答by ice.nicer

I would propose a change to the earlier response by Matt. His code will cause a mutation on the aobject. try this...

我建议更改 Matt 之前的回复。他的代码会导致a对象发生突变。尝试这个...

var moment = require('moment');
var a = moment('2013-01-01');
var b = moment('2013-06-01');

for (var m = moment(a); m.isBefore(b); m.add(1, 'days')) {
    console.log(m.format('YYYY-MM-DD'));
}

回答by nwaltham

Use the https://github.com/JerrySievert/node-date-utilsframework, then you can iterate easily like this:

使用https://github.com/JerrySievert/node-date-utils框架,然后你可以像这样轻松迭代:

require('date-utils');

var d = new Date('2013-01-01');
var e = new Date('2013-06-01');

for(var i = d; i.isBefore(e); i.addDays(1)) {
  console.log(i.toFormat("YYYY-MM-DD"));  
}

回答by Michael Minor

As much as there are many utilities for this, they might be cumbersome to integrate into a useful loop to check against data.

尽管有许多实用程序,但将它们集成到有用的循环中以检查数据可能很麻烦。

This should do the trick. It might be overkill, but you could very easily make this more argument based.

这应该可以解决问题。这可能有点矫枉过正,但你可以很容易地让这个更有论据。

var moment = require('moment');
var _ = require('lodash');

function(collectionsWithDateValues){
  var slots = [];
  var hours = {
    start: 7,   // 7am
    end: 21,    // 9pm
    window: 2   // How long each item should be slotted for.
  };
  var rightNow  = moment().add(0, 'days').hours(hours.start).minute(0).second(0);
  var cutoff    = moment(rightNow).add(14,'days'); // Check the next 2 weeks.
  for( rightNow ; rightNow.isBefore(cutoff) ; rightNow.add(hours.window, 'hours') ){
    // Check if we're going beyond the daily cutoff, go to the next day
    if(rightNow.isAfter(moment(rightNow).hour(hours.end))){
      rightNow.add(1, 'days').hour(hours.start);
    }
    var foundClash = false;
    _.forEach(collectionsWithDateValues,  function(item){
      // Check if the item is within now and the slotted time 
      foundClash = moment(item.date).isBetween(rightNow, moment(rightNow).add(hours.window, 'hours').subtract(1, 'minutes').seconds(59));
    });

    if(!foundClash){
      slots.push(rightNow.toString());
    }
  }
  return slots;
}