<view>{{dateFormat(date)}}</view>
data() {
	return {
		date: new Date().toISOString(),//现在时间
	}
},
onLoad() {
	let _this = this;
	setInterval(function() {
	_this.date = Date.parse(new Date());
	}, 1000);
},
methods: {
	dateFormat(time) {
	    let date = new Date(time);
	    let year = date.getFullYear();
	    // 在日期格式中,月份是从0开始的,因此要加0,使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
	    let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
	    let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
	    let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
	    let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
	    let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
	    // 拼接
	    return year + "年" + month + "月" + day + "日" + hours + ":" + minutes + ":" + seconds;
	    // return year + "-" + month + "-" + day;
	},
}

效果图:
在这里插入图片描述

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐