js倒计时与日期间隔计算

WEB前端 waitig 578℃ 百度已收录 0评论

js倒计时

/**
*[countDown日期,必填] 
*@param {a} 日期
*@param {b} 日期
*/
let a = '2017-10-10 11:51:00';
let b = '2017-10-24 12:51:00';
function countDown(start, end) {
    var distance = Math.abs(Date.parse(start) / 1000 - Date.parse(end) / 1000);
    var timer = setInterval(()=> {
        distance--;
        let obj = {
            "d":parseInt(distance / (24 * 60 * 60)),
            "h":parseInt((distance % (60 * 60 * 24)) / (60 * 60)),
            "m":parseInt((distance % (60 * 60)) / 60),
            "s":parseInt(distance % 60),
        };
        for(let k in obj){
            if(parseInt(obj[k]) < 10){
                obj[k] = '0' + obj[k]
            }
        }
        let str = `倒计时:${obj.d}天${obj.h}时${obj.m}分${obj.s}秒`;
        console.log(str)
        if(distance == 0){
            clearInterval(timer)
        }
    }, 1000)
}
countDown(a, b)

js日期间隔判断

/**
*[checkDate日期,天数,必填] 
*@param {val,days}日期,间隔天数
*@return [boolean] [是否超过日期间隔天数]
*/
function checkDate(val, days) {
    let bal = false;
    let allSec = days * 24 * 60 * 60;
    let tm = val.replace(' ','T');
    let checkSec = Date.parse(tm) / 1000;
    let nowSec = Date.parse(new Date()) / 1000;
    let interval = nowSec - checkSec;
    (interval > allSec) ? bal = false : bal = true;
    return bal
}
checkDate()

注意:实测Date.parse( ),new Date().getTime()转化’xxxx-xx-xx xx:xx:xx’格式的时间失效,需将’xxxx-xx-xx xx:xx:xx’转化为’xxxx-xx-xxTxx:xx:xx’


本文由【waitig】发表在等英博客
本文固定链接:js倒计时与日期间隔计算
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (0)分享 (0)