vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > vue获取当前时间,并且传递给后端

vue怎样获取当前时间,并且传递给后端(不用注解)

作者:最爱吃苹果

这篇文章主要介绍了vue怎样获得当前时间,并且传递给后端(不用注解)问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

vue获得当前时间,并且传递给后端

第一步

先获得当前时间

  var _this = this;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth()+1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let mf = new Date().getMinutes()<10 ? '0'+new Date().getMinutes() : new Date().getMinutes();
      let ss = new Date().getSeconds()<10 ? '0'+new Date().getSeconds() : new Date().getSeconds();

第二步

写用对象返回(我是给后端返回一个对象,所以在time前加了个messageboard,如果你没有直接返回time节ok)

  _this.messageBoard.time = yy+"-"+mm+"-"+dd;
      console.log(_this.messageBoard.time)

为什么不住注解

以下:后端要求的标准写法,如果你想要加小时,分钟,秒就按照下面的格式写。

standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSX", "yyyy-MM-dd'T'HH:mm:ss.SSS", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")

vue获取当前时间(年月日 时分秒)

代码实现

    // 获取当前时间
    const getGurrentTime = () => {
      let yy = new Date().getFullYear();
      let mm = (new Date().getMonth() + 1) < 10 ? '0' + (new Date().getMonth() + 1) : (new Date().getMonth() + 1);
      let dd = (new Date().getDate()) < 10 ? '0' + (new Date().getDate() + 1) : (new Date().getDate() + 1);
      let hh = new Date().getHours() < 10 ? '0' + new Date().getHours() : new Date().getHours();
      let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
      let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
      return  yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss;
    };

实现效果

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文