<script>exportdefault{data(){return{
nowDate:"",// 当前日期};},mounted(){this.currentTime();},
methods:{currentTime(){setInterval(this.formatDate,500);},formatDate(){
let date =newDate();
let year = date.getFullYear();// 年
let month = date.getMonth()+1;// 月
let day = date.getDate();// 日
let week = date.getDay();// 星期
let weekArr =["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
let hour = date.getHours();// 时
hour = hour <10?"0"+ hour : hour;// 如果只有一位,则前面补零
let minute = date.getMinutes();// 分
minute = minute <10?"0"+ minute : minute;// 如果只有一位,则前面补零
let second = date.getSeconds();// 秒
second = second <10?"0"+ second : second;// 如果只有一位,则前面补零this.nowDate = `${year}-${month}-${day} ${hour}:${minute}:${second}`;},}}</script>
第二种写法
export function getTime(){
var now =newDate();
var year=now.getFullYear();
var month=now.getMonth()+1;
var date=now.getDate();
var hour=now.getHours();
var minute=now.getMinutes();
var second=now.getSeconds();if(month<10){
month ='0'+month
}if(date<10){
date ='0'+date
}if(hour<10){
hour ='0'+hour
}if(minute<10){
minute ='0'+minute
}if(second<10){
second ='0'+second
}
let obj=year+'-'+month+'-'+date+' '+hour+':'+minute
return obj
}