首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多个日期范围的开始日期和结束日期

多个日期范围的开始日期和结束日期
EN

Stack Overflow用户
提问于 2010-02-11 19:46:10
回答 1查看 530关注 0票数 0

嗨,我的问题有点特殊,也可能不是。然而,问题是我在一个数组中解析日期范围,我需要找到范围中可能出现的开始日期和结束日期。我不知道我是否解释得很好,但是如果你需要更多的信息,请让我知道。

例如2010-7-11,2010-7-12,2010-7-13,2010-9-01,2010-9-02,....

现在

2010-7-11开始和2010-7-13结束

开始2010-9-01完

对于数组中的整个范围也是如此

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2010-02-11 20:06:13

这里有一些快速和下流的东西。它期望dates数组已经按升序排序。

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2244155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档