js时间倒计时和时间日期

倒计时

timeOut(value) {

let that = this;

let timer;

let orign_time = Date.parse(new Date()) / 1000;

let leftTime = value; //转化成秒,传入的是时间戳

let maxTime = leftTime - orign_time;

timer = setInterval(function CountDownTime() {

if (maxTime >= 0) {//判断传入的时间

that.days = parseInt(maxTime / 60 / 60 / 24, 10); //计算剩余的天数

that.hours = parseInt((maxTime / 60 / 60) % 24, 10); //计算剩余的小时

that.minutes = parseInt((maxTime / 60) % 60, 10); //计算剩余的分钟

that.seconds = parseInt(maxTime % 60, 10); //计算剩余的秒数

--maxTime;

} else {

console.log('时间到')

clearInterval(timer);

}

}, 1000);

},

timeOut(123456788999090)

时间日期即时钟

getNowtime() {

let that = this;//使用vue框架时,没有使用把that去掉

// let week = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];

let week = [

"星期日",

"星期一",

"星期二",

"星期三",

"星期四",

"星期五",

"星期六"

];

let timerID = setInterval(updateTime, 1000);

updateTime();

function updateTime() {

let cd = new Date();

that.time =

that.zhuanHua(cd.getHours(), 2) +

":" +

that.zhuanHua(cd.getMinutes(), 2) +

":" +

that.zhuanHua(cd.getSeconds(), 2);

that.date =

that.zhuanHua(cd.getFullYear(), 4) +

"年" +

that.zhuanHua(cd.getMonth() + 1, 2) +

"月" +

that.zhuanHua(cd.getDate(), 2) +

"日" +

" " +

week[cd.getDay()];

}

},

zhuanHua(num, digit) {//添加时间日期小于10时,添加一个0

let zero = "";

for (var i = 0; i < digit; i++) {

zero += "0";

}

return (zero + num).slice(-digit);

}

},

猜你喜欢

转载自blog.csdn.net/dwb123456123456/article/details/81916428