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

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

问题描述

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

我希望范围"是一组日期对象,两个日期之间的每一天都有一个.

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 - 获取两个日期之间的日期数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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