vue中获取当前时间

9 篇文章 0 订阅

vue组件(页面部分)

<template>
    <div class="monitor-time">
      {{nowTime}}
  </div>
</template>

方法调用
根据需要去调用方法
在data中定义nowTime:’’,

export default {
   data(){
       return {
          nowTime:'',
       }
   },
   mounted() {
     this.nowTimes();
   },
}

方法定义

nowTimes(){
      this.timeFormate(new Date());
      setInterval(this.nowTimes,1000);
      //this.clear()
    },
    //显示当前时间(年月日时分秒)
    timeFormate(timeStamp) {
      let year = new Date(timeStamp).getFullYear();
      let month =new Date(timeStamp).getMonth() + 1 < 10? "0" + (new Date(timeStamp).getMonth() + 1): new Date(timeStamp).getMonth() + 1;
      let date =new Date(timeStamp).getDate() < 10? "0" + new Date(timeStamp).getDate(): new Date(timeStamp).getDate();
      let hh =new Date(timeStamp).getHours() < 10? "0" + new Date(timeStamp).getHours(): new Date(timeStamp).getHours();
      let mm =new Date(timeStamp).getMinutes() < 10? "0" + new Date(timeStamp).getMinutes(): new Date(timeStamp).getMinutes();
      let ss =new Date(timeStamp).getSeconds() < 10? "0" + new Date(timeStamp).getSeconds(): new Date(timeStamp).getSeconds();
      let week = new Date(timeStamp).getDay();
      let weeks = ["日","一","二","三","四","五","六"];
      let getWeek = "星期" + weeks[week];
      this.nowTime = year + "年" + month + "月" + date +"日"+" "+hh+":"+mm+':'+ss+getWeek  ;
    },
    //clear(){
     // clearInterval(this.nowTimes)
     // this.nowTimes = null;
    //},

css部分(根据自己需要自定义即可)

.monitor-time{
    height:12px;
    float:right;
    margin:25px 15px 0 0 ;
    color:#27A5EA;
    font-size: 12px;
    font-family:FZLTCHK;
    line-height:27px;
  }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值