Javascript - 获取2个日期之间的日期数组 [英] Javascript - get array of dates between 2 dates

查看:158
本文介绍了Javascript - 获取2个日期之间的日期数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var range = getDates(new Date(), new Date().addDays(7));

我希望range成为一个日期对象数组,每天一对两个日期。

I'd like "range" to be an array of date objects, one for each day between the two dates.

诀窍是它应该处理月份和年份边界。

The trick is that it should handle month and year boundaries as well.

谢谢。

推荐答案

function (startDate, endDate, addFn, interval) {

 addFn = addFn || Date.prototype.addDays;
 interval = interval || 1;

 var retVal = [];
 var current = new Date(startDate);

 while (current <= endDate) {
  retVal.push(new Date(current));
  current = addFn.call(current, interval);
 }

 return retVal;

}

这篇关于Javascript - 获取2个日期之间的日期数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆