felix-cao / Blog

A little progress a day makes you a big success!
31 stars 4 forks source link

moment使用小知识 #213

Closed felix-cao closed 2 years ago

felix-cao commented 2 years ago

本周起始时间-结束时间

const thisWeek = () => {
    const endDate = moment().week(moment().week()).endOf('week').format(dateFormat);
    const startDate = moment().week(moment().week()).startOf('week').format(dateFormat);
    return { startDate, endDate }
}

上周起始时间-结束时间

  const lastMonth = () => {
    const endDate = moment().month(moment().month()).subtract(1,'months').endOf('month').format(dateFormat);
    const startDate = moment().month(moment().month()).subtract(1,'months').startOf('month').format(dateFormat);
    return { startDate, endDate }
  }

本月起始时间-结束时间

  const thisMonth = () => {
    const endDate = moment().month(moment().month()).endOf('month').format(dateFormat);
    const startDate = moment().month(moment().month()).startOf('month').format(dateFormat);
    return { startDate, endDate }
  }

上月起始时间-结束时间

  const lastMonth = () => {
    const endDate = moment().month(moment().month()).subtract(1,'months').endOf('month').format(dateFormat);
    const startDate = moment().month(moment().month()).subtract(1,'months').startOf('month').format(dateFormat);
    return { startDate, endDate }
  }