获取本月的所有日期

// 获取当月的天数
function getCountDays() {
  var curDate = new Date();
  /* 获取当前月份 */
  var curMonth = curDate.getMonth();
  /*  生成实际的月份: 由于curMonth会比实际月份小1, 故需加1 */
  curDate.setMonth(curMonth + 1);
  /* 将日期设置为0, 这里为什么要这样设置, 我不知道原因, 这是从网上学来的 */
  curDate.setDate(0);
  /* 返回当月的天数 */
  return curDate.getDate();
}
const numberDay = getCountDays()
// 获取当前的年/月
let time = new Date().getFullYear() +'/'+(new Date().getMonth()+1<10?'0'+new Date().getMonth()+1:new Date().getMonth()+1)
// 获取一个月的所有日期
let arrTime = []
for(var i =1;i<numberDay;i++) {
  console.log(i)
  arrTime.push(time+'/'+(i<10?'0'+i:i))
}
console.log(arrTime)

  

猜你喜欢

转载自www.cnblogs.com/js-liqian/p/11743490.html