<csript>
data(){
returen{
// 获取当前时间
nowDate() {
let nowDate = new Date();
let date = {
year: nowDate.getFullYear(),
month: nowDate.getMonth() + 1,
date: nowDate.getDate(),
hour: nowDate.getHours(),
min: nowDate.getMinutes(),
sec: nowDate.getSeconds(),
};
this.confirmTime =
date.year +
"-" +
date.month +
"-" +
date.date +
" " +
date.hour +
":" +
date.min +
":" +
date.sec;
},
}
}
mounted() {
this.nowDate();
},
<script/>
<template>
<div>
<p>当前时间:{{this.nowDate}}
<div/>
<template/>
day.js
npm install dayjs --save
引入
import dayjs from 'dayjs'
// 获取当前时间
nowDate() {
this.confirmTime =
dayjs().format('YYYY-MM-DD HH:mm:ss');
},