1:先上效果图:
2:注意点:
3:在组件中
今天的日期是:{{ FormatTime(nowTime) }}
4:script中代码段
- data() {
- return {
- timer: undefined,
- nowTime: new Date(),
- latedata:"2020-1-9",
- }
- },
-
- created() {
- // 显示时间,在渲染页面之前一直调用该函数,对this.time进行赋值
- this.timer = setInterval(() => {
- this.nowTime= new Date().toLocaleString();
- });
- //设置执行时间 一般1秒执行一次
- },
- // 销毁定时器
- beforeDestroy() {
- if (this.timer) {
- clearInterval(this.timer);
- }
- },
-
- methods: {
- FormatTime() {
- //返回显示的日期时间格式
- var date = new Date();
- var year = this.formatTime(date.getFullYear());
- var month = this.formatTime(date.getMonth() + 1);
- var day = this.formatTime(date.getDate());
- var hour = this.formatTime(date.getHours());
- var minute = this.formatTime(date.getMinutes());
- var second = this.formatTime(date.getSeconds());
- var weekday = date.getDay();
- var weeks = new Array(
- "星期日",
- "星期一",
- "星期二",
- "星期三",
- "星期四",
- "星期五",
- "星期六"
- );
- var week = weeks[weekday];
- return `${year}-${month}-${day} ${hour}:${minute}:${second} ${week}`;
- },
- formatTime(n) {
- //判断是否需要加0
- if (n < 10) {
- return "0" + n;
- } else {
- return n;
- }
- },
- }
formatTime(n)函数是为了对获取的每个demo进行格式转变,可有可无