嗨,我的问题有点特殊,也可能不是。然而,问题是我在一个数组中解析日期范围,我需要找到范围中可能出现的开始日期和结束日期。我不知道我是否解释得很好,但是如果你需要更多的信息,请让我知道。
例如2010-7-11,2010-7-12,2010-7-13,2010-9-01,2010-9-02,....
现在
2010-7-11开始和2010-7-13结束
开始2010-9-01完
对于数组中的整个范围也是如此
提前感谢
发布于 2010-02-11 20:06:13
这里有一些快速和下流的东西。它期望dates数组已经按升序排序。
var dates = ["2010-7-11", "2010-7-12", "2010-7-13", "2010-9-01", "2010-9-02"],
startDates = [], endDates = [],
lastDate = null, date = null;
for ( var i=0, l=dates.length; i<l; ++i ) {
date = new Date(dates[i].replace(/-/g, "/"));
//
if ( !lastDate ) {
startDates.push(lastDate = date);
}
// If the diffrence between the days is greater than the number
// of milliseconds in a day, then it is not consecutive
else if ( date - lastDate > 86400000 ) {
lastDate = null;
endDates.push(date);
}
}
// Close the last range
endDates.push(date);
// Result is two symetical arrays
console.log(startDates, endDates);https://stackoverflow.com/questions/2244155
复制相似问题