其他分享
首页 > 其他分享> > js 获取传入月份的月初月末

js 获取传入月份的月初月末

作者:互联网

/**
 * 默认当前月
 * @param date 带月份的日期,例:2021-8
 * @returns 月初至月末数组,例:[2021-8-1, 2021-8-31]
 */
function getMonthSE(date) {
  let nowdays = date ? new Date(date) : new Date();
  let year = nowdays.getFullYear();
  let month = nowdays.getMonth() + 1;

  if (month < 10) { month = "0" + month; }

  let myDate = new Date(year, month, 0);

  let startDate = year + "-" + month + "-01";
  let endDate = year + "-" + month + "-" + myDate.getDate();

  return [startDate, endDate];
}

标签:Date,传入,month,月末,let,year,date,new,js
来源: https://www.cnblogs.com/laodengtou/p/15770684.html