javascript – 获取两个date之间的date数组

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

我想“范围”是一个date对象的数组,每两天之间的一天。

诀窍是,它也应该处理月份和年份的界限。

谢谢。

 Date.prototype.addDays = function(days) { var date = new Date(this.valueOf()); date.setDate(date.getDate() + days); return date; } function getDates(startDate, stopDate) { var dateArray = new Array(); var currentDate = startDate; while (currentDate <= stopDate) { dateArray.push(new Date (currentDate)); currentDate = currentDate.addDays(1); } return dateArray; } 

这里是一个function演示 http://jsfiddle.net/jfhartsock/cM3ZU/

我使用了moment.js和Twix.js,它们为date和时间pipe理提供了非常好的支持

 var itr = moment.twix(new Date('2012-01-15'),new Date('2012-01-20')).iterate("days"); var range=[]; while(itr.hasNext()){ range.push(itr.next().toDate()) } console.log(range); 

我有这个运行在http://jsfiddle.net/Lkzg1bxb/

试试这个,记得要包括一下js,

 function getDates(startDate, stopDate) { var dateArray = []; var currentDate = moment(startDate); var stopDate = moment(stopDate); while (currentDate <= stopDate) { dateArray.push( moment(currentDate).format('YYYY-MM-DD') ) currentDate = moment(currentDate).add(1, 'days'); } return dateArray; } 
 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; } 
 var boxingDay = new Date("12/26/2010"); var nextWeek = boxingDay*1 + 7*24*3600*1000; function getDates( d1, d2 ){ var oneDay = 24*3600*1000; for (var d=[],ms=d1*1,last=d2*1;ms<last;ms+=oneDay){ d.push( new Date(ms) ); } return d; } getDates( boxingDay, nextWeek ).join("\n"); // Sun Dec 26 2010 00:00:00 GMT-0700 (Mountain Standard Time) // Mon Dec 27 2010 00:00:00 GMT-0700 (Mountain Standard Time) // Tue Dec 28 2010 00:00:00 GMT-0700 (Mountain Standard Time) // Wed Dec 29 2010 00:00:00 GMT-0700 (Mountain Standard Time) // Thu Dec 30 2010 00:00:00 GMT-0700 (Mountain Standard Time) // Fri Dec 31 2010 00:00:00 GMT-0700 (Mountain Standard Time) // Sat Jan 01 2011 00:00:00 GMT-0700 (Mountain Standard Time) 

我一直在使用@Mohammed Safeer解决scheme,并做了一些改进。 使用格式化date是在您的控制器中工作的不好的做法。 moment().format()只能用于视图中的显示目的。 还要记住, moment().clone()确保与input参数分离,这意味着inputdate不会改变。 我强烈build议您在使用date时使用moment.js。

用法:

  • 将moment.jsdate作为startDateendDate参数的值提供
  • interval参数是可选的,默认为“天”。 使用由.add()方法(moment.js)支持的间隔。 更多细节在这里
  • 以分钟为单位指定间隔时, total参数非常有用。 它默认为1。

调用:

 var startDate = moment(), endDate = moment().add(1, 'days'); getDatesRangeArray(startDate, endDate, 'minutes', 30); 

function:

 var getDatesRangeArray = function (startDate, endDate, interval, total) { var config = { interval: interval || 'days', total: total || 1 }, dateArray = [], currentDate = startDate.clone(); while (currentDate < endDate) { dateArray.push(currentDate); currentDate = currentDate.clone().add(config.total, config.interval); } return dateArray; }; 

我最近正在与moment.js合作,接下来是诀窍。

 function getDateRange(startDate, endDate, dateFormat) { var dates = [], end = moment(endDate), diff = endDate.diff(startDate, 'days'); if(!startDate.isValid() || !endDate.isValid() || diff <= 0) { return; } for(var i = 0; i < diff; i++) { dates.push(end.subtract(1,'d').format(dateFormat)); } return dates; }; console.log(getDateRange(startDate, endDate, dateFormat)); 

结果将是:

 ["09/03/2015", "10/03/2015", "11/03/2015", "12/03/2015", "13/03/2015", "14/03/2015", "15/03/2015", "16/03/2015", "17/03/2015", "18/03/2015"] 

d3js提供了许多方便的function,包括d3.time ,便于date操作

https://github.com/d3/d3-time

针对您的具体要求:

世界标准时间

 var range = d3.utcDay.range(new Date(), d3.utcDay.offset(new Date(), 7)); 

或当地时间

 var range = d3.timeDay.range(new Date(), d3.timeDay.offset(new Date(), 7)); 

范围将是date对象的数组,落在每一天的第一个可能的值上

你可以改变timeDay的timeHour,timeMonth等不同的时间间隔相同的结果

 var listDate = []; var startDate ='2017-02-01'; var endDate = '2017-02-10'; var dateMove = new Date(startDate); var strDate = startDate; while (strDate < endDate){ var strDate = dateMove.toISOString().slice(0,10); listDate.push(strDate); dateMove.setDate(dateMove.getDate()+1); }; console.log(listDate); //["2017-02-01", "2017-02-02", "2017-02-03", "2017-02-04", "2017-02-05", "2017-02-06", "2017-02-07", "2017-02-08", "2017-02-09", "2017-02-10"] 

这里是一个不需要任何库的单行代码,如果你不想创build另一个函数。 只需用你的variables或date值replacestartDate(在两个地方)和endDate(它们是jsdate对象)。 当然,如果你愿意,你可以把它包装在一个函数中

 Array(Math.floor((endDate - startDate) / 86400000) + 1).fill().map((_, idx) => (new Date(startDate.getTime() + idx * 86400000))) 

这可能有助于某人,

您可以从中获取行输出并根据需要设置row_date对象的格式。

 var from_date = '2016-01-01'; var to_date = '2016-02-20'; var dates = getDates(from_date, to_date); console.log(dates); function getDates(from_date, to_date) { var current_date = new Date(from_date); var end_date = new Date(to_date); var getTimeDiff = Math.abs(current_date.getTime() - end_date.getTime()); var date_range = Math.ceil(getTimeDiff / (1000 * 3600 * 24)) + 1 ; var weekday = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"]; var months = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"]; var dates = new Array(); for (var i = 0; i <= date_range; i++) { var getDate, getMonth = ''; if(current_date.getDate() < 10) { getDate = ('0'+ current_date.getDate());} else{getDate = current_date.getDate();} if(current_date.getMonth() < 9) { getMonth = ('0'+ (current_date.getMonth()+1));} else{getMonth = current_date.getMonth();} var row_date = {day: getDate, month: getMonth, year: current_date.getFullYear()}; var fmt_date = {weekDay: weekday[current_date.getDay()], date: getDate, month: months[current_date.getMonth()]}; var is_weekend = false; if (current_date.getDay() == 0 || current_date.getDay() == 6) { is_weekend = true; } dates.push({row_date: row_date, fmt_date: fmt_date, is_weekend: is_weekend}); current_date.setDate(current_date.getDate() + 1); } return dates; } 

https://gist.github.com/pranid/3c78f36253cbbc6a41a859c5d718f362.js