JS代码段:VUE下的时间,星期和年月日

2019-01-03 09:56:21来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

不为别的,只为以后复制粘贴方便

data() {
    return {
      date: "",
      time: "",
      week: ""
    };
  },

 

//vm.timeOffset是与服务器时间的偏移量,不用的话自行删掉

methods: {
    //获取当前时间
    getNowTime: function() {
      let vm = this;
      var nowLocalTime = Date.parse(new Date());
      var nowTime = new Date(nowLocalTime + vm.timeOffset);
      var year = nowTime.getFullYear();
      var mounth =
        nowTime.getMonth() + 1 < 10
          ? "0" + (nowTime.getMonth() + 1)
          : nowTime.getMonth() + 1;
      var date =
        nowTime.getDate() < 10 ? "0" + nowTime.getDate() : nowTime.getDate();
      vm.date = year + "-" + mounth + "-" + date;
      var hour =
        nowTime.getHours() < 10 ? "0" + nowTime.getHours() : nowTime.getHours();
      var minute =
        nowTime.getMinutes() < 10
          ? "0" + nowTime.getMinutes()
          : nowTime.getMinutes();
      vm.time = hour + ":" + minute;
      if (nowTime.getDay() == 0) {
        vm.week = "星期天";
      } else if (nowTime.getDay() == 1) {
        vm.week = "星期一";
      } else if (nowTime.getDay() == 2) {
        vm.week = "星期二";
      } else if (nowTime.getDay() == 3) {
        vm.week = "星期三";
      } else if (nowTime.getDay() == 4) {
        vm.week = "星期四";
      } else if (nowTime.getDay() == 5) {
        vm.week = "星期五";
      } else if (nowTime.getDay() == 6) {
        vm.week = "星期六";
      }
    }
  },
mounted: function() {
    let vm = this;
    vm.getNowTime();
    vm.currentTimeInterval = setInterval(vm.getNowTime(), 1000);
  }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:ios移动端禁止双指缩放功能

下一篇:短连接、长连接、轮询、长轮询、WebSocket