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];
}
上一篇:第3期-判断闰年


下一篇:java求俩个时间间隔的天数