vue 获取系统时间

1.我的需求:vue中获取系统时间,精确到秒。赋值给一个对象,传给后台存储到数据库中。

2.代码:

methods: {
    //获取系统时间函数
    timeFormat(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();
      return  year + "-" + month + "-" + date +" "+hh+":"+mm +":"+ss;

    },
    createBucket(){
      var type=document.getElementById("type").innerHTML;
      this.bucket.path = "E:/data/" + type + "/" + this.bucket.name;
       //调用并赋值
      this.bucket.create_time = this.timeFormat(new Date());
      console.log('create bucket' + JSON.stringify(this.bucket));
      this.creating = true;
      apiService.createBucket(this.bucket).then((result)=>{
          console.log(result);
          // success
          if(result.status === 201){
            this.bucket = result.data;
            this.showCreateMessage = true;
          }
            sleep(1000).then(() => {
               this.creating = false;
            })
      },(error)=>{
        this.showError = true;
            sleep(1000).then(() => {
               this.creating = false;
            })
      });
    },
    newBucket(){
      this.bucket = {};
    },
}

关于vue export default中方法之间的相互调用:https://www.jianshu.com/p/4c5d917f8b95

vue获取系统时间并显示:https://blog.csdn.net/cdx1170776994/article/details/82529332(我自己是根据这篇博客的代码按照自己的需求改的)

你可能感兴趣的:(vue 获取系统时间)