JS获取时间

187 阅读1分钟

1.获取前一天或几天的时间

 getDayBefore(day = 1) {
      // 获取当前时间
      const currentDate = new Date();

      // 将当前时间减去一天的毫秒数
      const previousDate = new Date(
        currentDate.getTime() - 24 * 60 * 60 * 1000 * day
      );

      // 获取前一天的年、月、日
      const previousYear = previousDate.getFullYear();
      const previousMonth = previousDate.getMonth() + 1; // 月份从0开始,因此需要加1
      const previousDay = previousDate.getDate();

      // 格式化前一天的时间字符串
      return `${previousYear}-${
        previousMonth > 10 ? previousMonth : '0' + previousMonth
      }-${previousDay}`;
    },

2.获取后一天或几天的时间

getDayAfter(day = 1) {
      // 获取当前时间
      const currentDate = new Date();

      // 将当前时间减去一天的毫秒数
      const previousDate = new Date(
        currentDate.getTime() + 24 * 60 * 60 * 1000 * day
      );

      // 获取前一天的年、月、日
      const previousYear = previousDate.getFullYear();
      const previousMonth = previousDate.getMonth() + 1; // 月份从0开始,因此需要加1
      const previousDay = previousDate.getDate();

      // 格式化前一天的时间字符串
      return `${previousYear}-${
        previousMonth > 10 ? previousMonth : '0' + previousMonth
      }-${previousDay}`;
    },

3.获取上个月的时间

getLastMonth() {
      // 获取当前时间
      const currentDate = new Date();

      // 获取当前月份
      let currentMonth = currentDate.getMonth();

      // 减去一个月
      currentMonth--;

      // 处理特殊情况
      if (currentMonth < 0) {
        currentMonth = 11; // 将月份调整为十二月
        currentDate.setFullYear(currentDate.getFullYear() - 1); // 年份减去一年
      }

      // 设置日期为上个月的最后一天
      currentDate.setMonth(currentMonth + 1, 0);

      // 获取上个月的年、月、日
      const previousYear = currentDate.getFullYear();
      const previousMonth = currentDate.getMonth() + 1; // 月份从0开始,因此需要加1
      const previousDay = currentDate.getDate();

      // 格式化上个月的时间字符串
       const previousMonthString = `${previousYear}-${previousMonth}-${previousDay}`;

      return {
        firstTime: `${previousYear}-${previousMonth}-1`,
        lastTime: previousMonthString,
      };
    },

4.获取当月天数以及当月第一天星期

function getDayCounts(year, month) {
  // 获取总天数
  const total = new Date(year, month, 0).getDate();
  // 获取第一天星期
  const firstWeekDay = new Date(year, month - 1, 1).getDay();
  return { total, firstWeekDay };
}

5.判断是否是闰年

function isLeapYear(year) {
  //    能够被4整除,但不能被100整除。
  //    能够被400整除。
  // return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;

  return (
    (Number.isInteger(year / 4) && !Number.isInteger(year / 100)) ||
    Number.isInteger(year / 400)
  );
}

6.获取一年当中的每一天

//当月的天数
function getDaysInMonth(year, month) {
  const daysInMonth = new Date(year, month, 0).getDate();
  const daysArray = [];
  for (let day = 1; day <= daysInMonth; day++) {
    daysArray.push(`${year}-${padZero(month)}-${padZero(day)}`);
  }
  return daysArray;
}
//每年的月数
function getAllYear() {
  const arr = [];
  for (let i = 1; i <= 12; i++) {
    const res = getDaysInMonth(2020, i);
    arr.push(...res);
  }
  return arr;
}
// 补0
function padZero(number) {
  return number > 9 ? number : `0${number}`;
}

7.年份按照一周排列

// 例如:2020-01-01 至 2020-01-05 2020-01-06-12