距离当前时间前多少天&&获取某年某月 有多少天

2021-04-28  本文已影响0人  放逐的帝王
timeStampToFormdate(timestamp) {
      if (!timestamp) {
        return '';
      }
      let date =
        timestamp.toString().length == '13'
          ? new Date(timestamp)
          : new Date(timestamp * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
      let Y = date.getFullYear() + '-';
      let M =
        (date.getMonth() + 1 < 10
          ? '0' + (date.getMonth() + 1)
          : date.getMonth() + 1) + '-';
      let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
      let h =
        date.getHours() < 10
          ? '0' + date.getHours() + ':'
          : date.getHours() + ':';
      let m =
        date.getMinutes() < 10
          ? '0' + date.getMinutes() + ':'
          : date.getMinutes() + ':';
      let s =
        date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
      return Y + M + D + ' ' + h + m + s;
    },
    //距离当前时间前多少天
    manyDayBeforeNowDay(days) {
      if (days * 1) {
        return this.timeStampToFormdate(
          new Date().getTime() - days * 60 * 60 * 24 * 1000
        );
      } else {
        return '';
      }
    },

使用简单 : let theDay = manyDayBeforeNowDay(30); theDay 就是距离当前时间 30天的日期

获取某年某月 有多少天

getMonth(month) {
      let d = new Date();
      let day = d.getDate(); //获取当前日期中的月的天数
      let year2 = d.getFullYear(); //获取当前日期中的年
      //   let month = d.getMonth() + 1; //获取当前日期中的月
      let month2 = parseInt(month);
      if (month2 == 13) {
        year2 = parseInt(year2) + 1;
        month2 = 1;
      }
      if (month2 < 10) {
        month2 = '0' + month2;
      }
      let curMonthDays = new Date(year2, month2, 0).getDate();
      let t2 = year2 + '-' + month2 + '-' + curMonthDays;
      return curMonthDays;
    },

上面的代码是 今年 传出月份,返回 值是传入月份的天数

上一篇 下一篇

猜你喜欢

热点阅读