js获取当前的年月日和一周前的年月日

2021-10-28  本文已影响0人  tangzhentian
      //现在的时间
      var dates = new Date();
      let year = dates.getFullYear();
      let mounth = (dates.getMonth() + 1).toString().padStart(2,'0');
      let day = dates.getDate().toString().padStart(2,'0');
      let hour = dates.getHours().toString().padStart(2,'0');
      let minutes = dates.getMinutes().toString().padStart(2,'0');
      // 2021-10-28 09:57
      console.log(year+'-'+mounth+'-'+day+' '+hour+':'+minutes);
      // 一周前的时间
      var time=(new Date()).getTime()-7*24*60*60*1000;
      var tragetTime=new Date(time);
      let tyear = tragetTime.getFullYear();
      let tmounth = (tragetTime.getMonth() + 1).toString().padStart(2,'0');
      let tday = tragetTime.getDate().toString().padStart(2,'0');
      let thour = tragetTime.getHours().toString().padStart(2,'0');
      let tminutes = tragetTime.getMinutes().toString().padStart(2,'0');
      // 2021-10-21 09:57
      console.log(tyear+'-'+tmounth+'-'+tday+' '+thour+':'+tminutes);
上一篇下一篇

猜你喜欢

热点阅读