我想从数组的静态日期创建日期范围 这是我想要的一个例子:
var collDates = [
"2017/01/01",
"2017/01/02",
"2017/01/03",
"2017/01/04",
"2017/01/08",
"2017/01/09"
];
这应该转换为:
[
{ start_date: "2017/01/01", end_date: "2017/01/04" },
{ start_date: "2017/01/08", end_date: "2017/01/09" }
];
答案 0 :(得分:1)
我已经解决了这个问题,这是一个示例代码。
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);
答案 1 :(得分:0)
没有库,您只需要几个简单的函数来进行解析和格式化。对无效日期不做任何事情,但如果需要则不难添加。
逐步完成日期以创建范围。从开始日期开始,将第二天计算为当前日期加1。获得不是第二天的日期时,结束范围并开始新的日期。也是在约会用完时结束。
对于单个日期,创建一个日期范围。
/* 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
无法看到这与jQuery有什么关系。没有moment.js标签,所以我没有使用它。 ; - )