vue实现指定日期之间的倒计时

这篇文章主要为大家详细介绍了vue实现指定日期之间的倒计时,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了vue实现指定日期之间倒计时的具体代码,供大家参考,具体内容如下

效果图如下

此处使用moment.js日期处理类库 使用方法如下

npm install moment 或者 yarn add moment

html

 
{{dayNum}}
{{hourNum}}
{{minuteNum}}
{{secondNum}}

js

 import moment from 'moment'; export default { name: 'TimeRangPage', props: { startTime: String, endTime: String }, data () { return { days: 0, hours: 0, minutes: 0, seconds: 0, timeSetInterval: null, showTimeDown: false, showOver: false }; }, created () { if (moment(new Date()).isBefore(this.startTime)) { this.showTimeDown = true; this.timeDown(); } if (moment(new Date()).isAfter(this.endTime)) this.showOver = true; }, methods: { timeDown () { this.timeSetInterval = setInterval(() => { if (moment(this.startTime).isBefore(moment())) { this.showTimeDown = false; clearInterval(this.timeSetInterval); location.reload(); } let dur = moment.duration(moment(this.startTime) - moment(), 'ms'); this.days = dur.get('days'); this.hours = dur.get('hours'); this.minutes = dur.get('minutes'); this.seconds = dur.get('seconds'); }, 1000); } }, computed: { dayNum () { if (this.days <10) return '0' + this.days; return this.days; }, hourNum () { if (this.hours <10) return '0' + this.hours; return this.hours; }, minuteNum () { if (this.minutes <10) return '0' + this.minutes; return this.minutes; }, secondNum () { if (this.seconds <10) return '0' + this.seconds; return this.seconds; } } };

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。

以上就是vue实现指定日期之间的倒计时的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Vue.js 教程