moment.js 获取国内自燃周开始时间与结束时间以及当前日期第几周

/**
 * 国内自然周
 */
const getWeekOfNature=(date,format)=>{
  let weekOfday = moment(date).format("E"); //计算是这周第几天
  let monday = moment(date).subtract(weekOfday - 1, "days"); //获取周一
  let sunday = moment(date).add(7 - weekOfday, "days"); // 获取周日
  return {
    week:moment(monday).format("WW"),
    startDate: format ?monday.format(format): monday,
    endDate:format ?sunday.format(format): sunday,
  }
}
console.log(getWeekOfNature('2022-10-07','YYYY-MM-DD')) // {week: '40', startDate: '2022-10-03', endDate: '2022-10-09'}

 

posted @ 2022-12-07 13:23  浮生如梦似离殇  阅读(363)  评论(0编辑  收藏  举报