js获取当前日期或者指定日期的昨天、上周、上月

21 篇文章 0 订阅

开发中经常用到对时间进行处理,获取昨日、上周、上月的日期;一下是封装的方法:

获取昨日日期:

// 获取昨日日期
  getLastDate = () => {
    const day = new Date();
    const lastDay = new Date(day.setTime(day.getTime() - (24 * 60 * 60 * 1000)));
    return this.formatDate(lastDay);
  };

获取上一周的日期:

// 获取上一周的方法
getLastWeek = () => {
    const day = new Date();// 获取当前时间
    const week = day.getDay();
    const millisecond = 1000 * 60 * 60 * 24; // 一天的毫秒数
    const minusDay = week !== 0 ? week - 1 : 6; // 减去的天数
    // 获得当前周的第一天
    const currentWeekDayOne = new Date(day.getTime() - (millisecond * minusDay));
    // 上周最后一天即本周开始的前一天
    const priorWeekLastDay = new Date(currentWeekDayOne.getTime() - millisecond);
    // 上周的第一天
    const priorWeekFirstDay = new Date(priorWeekLastDay.getTime() - (millisecond * 6));
    // 添加至数组
    const firstWeek = this.formatDate(priorWeekFirstDay);
    const endWeek = this.formatDate(priorWeekLastDay);
    return `${firstWeek}~${endWeek}`;
  };

获取上月日期:

// 获取上月日期
  getLastMonth = () => {
    const day = new Date();
    // 获得当前月份0-11
    const currentMonth = day.getMonth();
    // 获得当前年份4位年
    const currentYear = day.getFullYear();
    // 获得上一个月的第一天
    const priorMonthFirstDay = this.getPriorMonthFirstDay(currentYear, currentMonth);
    // 获得上一月的最后一天
    const priorMonthLastDay = new Date(priorMonthFirstDay.getFullYear(),
      priorMonthFirstDay.getMonth(),
      this.getMonthDays(priorMonthFirstDay.getFullYear(),
        priorMonthFirstDay.getMonth()));
    const firstMonth = this.formatDate(priorMonthFirstDay);
    const endMonth = this.formatDate(priorMonthLastDay);
    return `${firstMonth}~${endMonth}`;
  };

公用函数:

获取该月的天数:

// 获得该月的天数
  getMonthDays = (year, month) => {
    // 本月第一天 1-31
    const relativeDate = new Date(year, month, 1);
    // 获得当前月份0-11
    let relativeMonth = relativeDate.getMonth();
    // 获得当前年份4位年
    let relativeYear = relativeDate.getFullYear();

    // 当为12月的时候年份需要加1 月份需要更新为0 也就是下一年的第一个月
    if (relativeMonth === 11) {
      relativeYear += 1;
      relativeMonth = 0;
    } else {
      // 否则只是月份增加,以便求的下一月的第一天
      relativeMonth += 1;
    }
    // 一天的毫秒数
    const millisecond = 1000 * 60 * 60 * 24;
    // 下月的第一天
    const nextMonthDayOne = new Date(relativeYear, relativeMonth, 1);
    // 返回得到上月的最后一天,也就是本月总天数
    return new Date(nextMonthDayOne.getTime() - millisecond).getDate();
  };

返回上月第一天date

 // 返回上一个月的第一天Date类型
  getPriorMonthFirstDay = (year, month) => {
    // 年份为0代表,是本年的第一月,所以不能减
    if (month === 0) {
      return new Date(year - 1, 11, 1);
    }
    // 否则,只减去月份
    return new Date(year, month - 1, 1);
  };

格式化日期:

  // 格式化日期
  formatDate = (date) => {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    return `${year}/${month}/${day}`;
  }

获取指定日期多少天后的日期:

 getSomeDayDate = (date, num, interval) => {
  const nowDate = new Date(date);
  if (interval === 'after') {
    nowDate.setDate(nowDate.getDate() + num);
  } else if (interval === 'before') {
    nowDate.setDate(nowDate.getDate() - num);
  }
  const year = nowDate.getFullYear();
  const month = nowDate.getMonth() + 1 < 10 ? `0${(nowDate.getMonth() + 1)}` : nowDate.getMonth() + 1;
  const day = nowDate.getDate() < 10 ? `0${nowDate.getDate()}` : nowDate.getDate();
  return `${year}-${month}-${day}`;
}

update 2020/1/14, message: 获取当前日期前后指定天数后的日期:


// 获取指定天数前几天的日期
function getDay(day) {
  const today = new Date();
  const dayToSeconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
  today.setTime(dayToSeconds); //注意,这行是关键代码
  const tYear = today.getFullYear();
  let tMonth = today.getMonth();
  let tDate = today.getDate();
  tMonth = doHandleMonth(tMonth + 1);
  tDate = doHandleMonth(tDate);
  return `${tYear}-${tMonth}-${tDate}`;
}

function doHandleMonth(month) {
  var m = month;
  if (month.toString().length == 1) {
    m = "0" + month;
  }
  return m;
}

eg:

console.log(getDay(-7));
// 2020-1-7
console.log(getDay(7));
// 2020-1-21

小伙伴们感觉有帮助的话希望点个赞哟,如果有问题或疑问欢迎大家评论区留言,看到后第一时间回复。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值