js 秒数转化为 日期 小时 分钟 秒格式,倒计时功能

data() {
		return {
			count: '',
			day: '',
			hour: '',
			minutes: '',
			secondtrue: ''
		};
	},
	mounted() {
		this.count = this.second;
		this.$nextTick(() => {
			this.handlecount();
		});
		setInterval(() => {
			this.count--;
		}, 1000);
	},
	watch: {
		count(nval) {
			this.handlecount();
			this.$nextTick(() => {
				this.addzero();
			});
		}
	},
	methods: {
		handlecount() {
			if (this.count < 86400) {
				this.day = 0;
				if (this.count < 3600) {
					this.hour = 0;
					if (this.count < 60) {
						this.minutes = 0;
						this.secondtrue = this.count;
					} else {
						this.minutes = Math.floor(this.count / 60);
						this.secondtrue = this.count - this.minutes * 60;
					}
				} else {
					this.hour = Math.floor(this.count / 3600);
					if (this.count - this.hour * 3600 < 60) {
						this.minutes = 0;
						this.secondtrue = this.count - this.hour * 3600;
					} else {
						this.minutes = Math.floor((this.count - this.hour * 3600) / 60);
						this.secondtrue = this.count - this.hour * 3600 - this.minutes * 60;
					}
				}
			} else {
				this.day = Math.floor(this.count / 86400);
				if (this.count - 86400 * this.day < 3600) {
					this.hour = 0;
					this.minutes = Math.floor((this.count - 86400 * this.day) / 60);
					this.secondtrue = this.count - 86400 * this.day - 60 * this.minutes;
				} else {
					this.hour = Math.floor((this.count - 86400 * this.day) / 3600);
					if (this.count - 86400 * this.day - 3600 * this.hour < 60) {
						this.minutes = 0;
						this.secondtrue = this.count - 86400 * this.day - 3600 * this.hour;
					} else {
						this.minutes = Math.floor((this.count - 86400 * this.day - 3600 * this.hour) / 60);
						this.secondtrue = this.count - 86400 * this.day - 3600 * this.hour - 60 * this.minutes;
					}
				}
			}
		},
		addzero() {
			if (this.day < 10) {
				this.day = '0' + this.day;
			}
			if (this.minutes < 10) {
				this.minutes = '0' + this.minutes;
			}
			if (this.hour < 10) {
				this.hour = '0' + this.hour;
			}
			if (this.secondtrue < 10) {
				this.secondtrue = '0' + this.secondtrue;
			}
		}
	}

感觉写的有些low,但是实测还是非常稳的,到0秒的时候加一个清楚计时器的函数就可以了,如果nextTick用不了的同学,可以换别的方法在值小于10的时候在前面加0,

Logo

前往低代码交流专区

更多推荐