JS获取本周、上周、本月、上月时间范围

1,820 阅读2分钟

获取上周时间段

传参要求:
// interface DateTypeFunc {
//   (value?: null | undefined | number | Date, separate?: string): { startDateTime: string; endDateTime: string };
// }

const getLastWeek = (value = null, separate = '-') => {
  // 如果为无效时间,则格式化当前时间
  if (!value) value = +new Date();
  // 如果dateTime长度为10或者13,则为秒和毫秒的时间戳,如果超过13位,则为其他的时间格式
  if (String(value).length == 10 && value == Number(value)) value *= 1000;
  value = Number(value);

  const one_day = 86400000; // 24 * 60 * 60 * 1000;
  const lastWeekDate = new Date(value - 7 * one_day);
  const day = lastWeekDate.getDay() === 0 ? 7 : lastWeekDate.getDay(); // 返回1-7,7表示周日
  // 设置时间为上周那天的0点
  lastWeekDate.setHours(0, 0, 0, 0);

  //算出上周开始时间结束时间
  const week_start_time = new Date(lastWeekDate.getTime() - (day - 1) * one_day);
  const week_end_time = new Date(lastWeekDate.getTime() + (7 - day) * one_day);

  //格式化日期
  const filters = n => {
    return n < 10 ? (n = '0' + n) : n;
  };
  const startmonth = filters(week_start_time.getMonth() + 1);
  const startDay = filters(week_start_time.getDate());
  const endmonth = filters(week_end_time.getMonth() + 1);
  const endDay = filters(week_end_time.getDate());

  const startDateTime = week_start_time.getFullYear() + separate + startmonth + separate + startDay;
  const endDateTime = week_end_time.getFullYear() + separate + endmonth + separate + endDay;

  return {
    startDateTime: startDateTime + ' 00:00:00',
    endDateTime: endDateTime + ' 23:59:59',
  };
};

获取本周时间段

const getCurrentWeek = (value = null, separate = '-') => {
  // 如果为无效时间,则格式化当前时间
  if (!value) value = +new Date();
  // 如果dateTime长度为10或者13,则为秒和毫秒的时间戳,如果超过13位,则为其他的时间格式
  if (String(value).length == 10 && value == Number(value)) value *= 1000;
  value = Number(value);

  const one_day = 86400000; // 24 * 60 * 60 * 1000;
  const weekDate = new Date(value);
  const day = weekDate.getDay() === 0 ? 7 : weekDate.getDay(); // 返回1-7,7表示周日
  // 设置时间为当天的0点
  weekDate.setHours(0, 0, 0, 0);

  //算出本周开始时间结束时间
  const week_start_time = new Date(weekDate.getTime() - (day - 1) * one_day);
  const week_end_time = new Date(weekDate.getTime() + (7 - day) * one_day);

  //格式化日期
  const filters = n => {
    return n < 10 ? (n = '0' + n) : n;
  };
  const startmonth = filters(week_start_time.getMonth() + 1);
  const startDay = filters(week_start_time.getDate());
  const endmonth = filters(week_end_time.getMonth() + 1);
  const endDay = filters(week_end_time.getDate());

  const startDateTime = week_start_time.getFullYear() + separate + startmonth + separate + startDay;
  const endDateTime = week_end_time.getFullYear() + separate + endmonth + separate + endDay;
  return {
    startDateTime: startDateTime + ' 00:00:00',
    endDateTime: endDateTime + ' 23:59:59',
  };
};

获取上个月时间段

const getLastMonth = (value = null, separate = '-') => {
  // 如果为无效时间,则格式化当前时间
  if (!value) value = +new Date();
  // 如果dateTime长度为10或者13,则为秒和毫秒的时间戳,如果超过13位,则为其他的时间格式
  if (String(value).length == 10 && value == Number(value)) value *= 1000;
  value = Number(value);

  // 获取上个月时间
  const targetTime = new Date(value);
  let year = targetTime.getFullYear();
  let month = targetTime.getMonth();
  if (month === 0) {
    month = 12;
    year = year - 1;
  }
  if (month < 10) {
    month = '0' + month;
  }

  const yDate = new Date(year, Number(month), 0);

  const startDateTime = year + separate + month + separate + '01 00:00:00'; //上个月第一天
  const endDateTime = year + separate + month + separate + yDate.getDate() + ' 23:59:59'; //上个月最后一天
  return {
    startDateTime: startDateTime,
    endDateTime: endDateTime,
  };
};

获取本月时间段

const getCurrentMonth = (value = null, separate = '-') => {
  // 如果为无效时间,则格式化当前时间
  if (!value) value = +new Date();
  // 如果dateTime长度为10或者13,则为秒和毫秒的时间戳,如果超过13位,则为其他的时间格式
  if (String(value).length == 10 && value == Number(value)) value *= 1000;
  value = Number(value);

  const targetTime = new Date(value);
  const year = targetTime.getFullYear();
  let month = targetTime.getMonth() + 1;
  if (month < 10) {
    month = '0' + month;
  }

  const yDate = new Date(year, Number(month), 0);

  const startDateTime = year + separate + month + separate + '01 00:00:00'; //这个月第一天
  const endDateTime = year + separate + month + separate + yDate.getDate() + ' 23:59:59'; //这个月最后一天

  return {
    startDateTime: startDateTime,
    endDateTime: endDateTime,
  };
};

例子:

console.log(getLastWeek());
console.log(getLastWeek(null, '/'));
console.log(getLastWeek(1634203654666));
console.log(getLastWeek(new Date('2021/10/14 05:00:00')));