倒计时

 function TimeDown(params) {
        // console.log(endDateStr)
        //结束时间
        let endDate = new Date(endDateStr.replace(/-/g,"/"));
        //当前时间
        let nowDate = new Date();
        //相差的总秒数
        let totalSeconds = parseInt((endDate - nowDate) / 1000);
        //天数
        let days = Math.floor(totalSeconds / (60 * 60 * 24));
        //取模(余数)
        let modulo = totalSeconds % (60 * 60 * 24);
        //小时数
        let hours = Math.floor(modulo / (60 * 60));
        modulo = modulo % (60 * 60);
        //分钟
        let minutes = Math.floor(modulo / 60);
        //秒
        let seconds = modulo % 60;

        console.log(totalSeconds);
        if (totalSeconds<0) {
            this.setState({recoverTime:"0分0秒后"})
            return;
        }
        //输出到页面
        this.setState({recoverTime:days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒"})
        //延迟一秒执行自己
        setTimeout( () => {
            this.TimeDown(endDateStr);

        }, 1000)
    }

//函数调用,输入string类型的时间格式
    TimeDown('2021-01-01 00:00:000')

 

你可能感兴趣的:(时间)