JS判断日期间的间隔天数(除节假日)

<html>
<body>
<script type="text/javascript">
    arrs = ["01.01", "05.01", "10.01", "10.02", "10.03", "2015.04.05", "2015.06.20", "2015.09.27", "2015.02.19", "2015.02.20", "2015.02.21", "2016.04.04", "2016.06.09", "2016.09.15", "2016.02.08", "2016.02.09", "2016.02.10"];
    var dStr = ',' + arrs.join(',') + ',';
    function padding(v) { if (v < 10) return '0' + v; return v.toString() }
    function getEndDate(start, day) {
        start = new Date(start.replace(/./g, '/').replace(/-/g, '/'));
        var s = '';
        while (true) {
            s = padding(start.getMonth() + 1) + '.' + padding(start.getDate());
            if (
                '06'.indexOf(start.getDay()) == -1 &&//周末
                dStr.indexOf(',' + s + ',') == -1 && dStr.indexOf(',' + start.getFullYear() + '.' + s + ',') == -1//节日
                ) {
                day--;
            }
 
            if (day == 0) break;
            start.setDate(start.getDate() + 1);
        }
        return start.getFullYear() + '.' + padding(start.getMonth() + 1) + '.' + padding(start.getDate());
    }
    function getWorkDay(start, end) {
        start = new Date(start.replace(/./g, '/').replace(/-/g, '/'));
        end = new Date(end.replace(/./g, '/').replace(/-/g, '/')).getTime();
        var day = 0;
        while (start.getTime() <= end) {
            s = padding(start.getMonth() + 1) + '.' + padding(start.getDate());
            if (
                '06'.indexOf(start.getDay()) == -1 &&//周末
                dStr.indexOf(',' + s + ',') == -1 && dStr.indexOf(',' + start.getFullYear() + '.' + s + ',') == -1//节日
                )
                day++;
            start.setDate(start.getDate() + 1);
        }
 
        return day;
    }
    alert(getEndDate('2015.12.30', 3))
    alert(getWorkDay('2017-11-22', '2017-11-23'))
 
</script>
</body>
</html>


by 雪洁 2018-06-19 07:56:10 517 views
我来说几句

相关文章