欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-03 11:42:03
不为别的,只为以后复制粘贴方便 //vm.timeOffset是与服务器时间的偏移量,不用的话自行删掉 ......

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

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);
  }