[英]How to get date ranges from an array of static dates in jquery or javascript
I want to create date ranges from statics dates of array. 我想从数组的静态日期创建日期范围。
Here is an example of what I want: 这是我想要的示例:
var collDates = [
"2017/01/01",
"2017/01/02",
"2017/01/03",
"2017/01/04",
"2017/01/08",
"2017/01/09"
];
This should be transformed to: 这应该转换为:
[
{ start_date: "2017/01/01", end_date: "2017/01/04" },
{ start_date: "2017/01/08", end_date: "2017/01/09" }
];
I have resolved the issue and here is an example code. 我已经解决了这个问题,这是示例代码。
var staticDates = ["2017/01/01", "2017/01/02", "2017/01/03", "2017/01/04", "2017/01/08", "2017/01/09", "2017/01/10", "2017/01/11", "2017/01/12", "2017/01/13", "2017/01/14", "2017/01/15", "2017/01/16", "2017/01/17", "2017/01/18", "2017/01/19", "2017/01/20", "2017/01/21", "2017/01/22", "2017/01/23", "2017/01/24", "2017/01/25", "2017/01/26", "2017/01/27", "2017/01/28", "2017/01/29"];
var coll_dateIntervals = [];
var arr_temp = [];
var i = 1;
$.each(staticDates, function(index, moment_date){
//Day difference in # of days
var diff = Math.abs(moment(staticDates[index]).diff(staticDates[index + 1], "days"));
arr_temp.push(moment_date);
//Check the date difference in days.
if(diff <= 1 && diff !== undefined){
//If the difference is 1, than add the date to the temporary array
arr_temp.push(moment_date);
//If it's more than 1 day, or the last object
} else if (diff > 1 || diff === undefined){
//Store the interval in an object
console.log(arr_temp[arr_temp.length - 1]);
var obj_dateInterval = { start_date: moment(arr_temp[0], "YYYY/MM/DD").format('YYYY-MM-DD'), end_date: moment(arr_temp[arr_temp.length - 1], "YYYY/MM/DD").format('YYYY-MM-DD')};
coll_dateIntervals.push(obj_dateInterval);
//Empty the array to start the new loop
arr_temp = [];
}
});
console.log(coll_dateIntervals);
Without a library, you just need a couple of simple functions for parsing and formatting. 没有库,您只需要几个简单的函数即可进行解析和格式化。 Doesn't do anything with invalid dates, but not difficult to add if required. 对于无效的日期不做任何事情,但是如果需要的话,添加也不难。
Step through the dates to create ranges. 逐步浏览日期以创建范围。 Begin with a start date, calculate the next day as current date plus one. 从开始日期开始,将第二天计算为当前日期加一。 When get a date that isn't the next day, end the range and start a new one. 当得到的日期不是第二天时,请结束范围并开始新的日期。 Also end when run out of dates. 过期时也结束。
For single dates, creates a single date range. 对于单个日期,创建一个单个日期范围。
/* Parse date in format y/m/d ** @param {string} s - string to parse ** @returns {Date} */ function parseYMD(s) { var b = s.split(/\\D/); return new Date(b[0], b[1] - 1, b[2]) } /* Collate array of date strings in y/m/d format to ranges ** @param {Array} dateArray - array of dates to parse in y/m/d format ** @returns {Array} ranges in format {start_date: yyyy/mm/dd, end_date: yyyy/mm/dd} */ function datesToRanges(dateArray) { return dateArray.reduce(function(acc, currentDate, index) { var d = parseYMD(currentDate); // Start of a processing, initialise accumulator if (!acc) { acc = {range: {start_date: currentDate}, ranges: []}; // If not continuing range, end current range, store and start new } else if (acc.previousDate.setDate(acc.previousDate.getDate() + 1) != +d) { acc.range.end_date = acc.previousString; acc.ranges.push(acc.range); acc.range = {start_date: currentDate}; } // Keep current values for next iteration acc.previousDate = d; acc.previousString = currentDate; // If at end of dates, end current range if (index == dateArray.length - 1) { acc.range.end_date = currentDate; acc.ranges.push(acc.range); } return acc; }, null).ranges; } // Tests var collDates = [ "2017/01/01", // Start 4 day range "2017/01/02", "2017/01/03", "2017/01/04", // End 4 day range "2017/01/06", // Zero day range "2017/01/08", // Start one day range "2017/01/09", // End one day range "2017/01/15", // Zero day range ]; console.log(datesToRanges(collDates));
PS 聚苯乙烯
Can't see what this has to do with jQuery. 无法看到这与jQuery有什么关系。 There's no moment.js tag so I haven't used it. 没有moment.js标记,因此我没有使用过。 ;-) ;-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.