获取当月天数

  1. getTotalDay(year, month) {
  2. const list = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  3. if ((!(year % 4) && (year % 100) || !(year % 400))) {
  4. // 闰年
  5. list[1] = 29;
  6. }
  7. return list[month];
  8. }
  9. const time = new Date();
  10. const days = new Date(time.getFullYear(), time.getMonth(),0);

获取工作日

  1. getNoWeekendDateList(length, haveToday = true) {
  2. const time = new Date(2021, 2, 28);
  3. const currentYear = time.getFullYear();
  4. const currentMonth = time.getMonth() + 1;
  5. const currentDate = time.getDate();
  6. const currentTotalDay = this.getTotalDay(currentYear, currentMonth);
  7. const dayList = () => {
  8. let tempList;
  9. if (haveToday) {
  10. tempList = [...Array(currentTotalDay - currentDate + 1)].map((e, i) => i + currentDate);
  11. } else {
  12. tempList = [...Array(currentTotalDay - currentDate)].map((e, i) => i + currentDate + 1);
  13. }
  14. const gap = length + 2 - tempList.length
  15. if (gap > 0) {
  16. tempList.push(...[...Array(gap)].map((e, i) => i + 1).filter(i => ![0, 6].includes(new Date(currentYear, currentMonth, i).getDay()),
  17. ));
  18. console.log('gap', gap);
  19. }
  20. console.log('tempList', tempList);
  21. return tempList;
  22. };
  23. return dayList().filter(i => ![0, 6].includes(new Date(currentYear, currentMonth - 1, i).getDay())).slice(0, 6);
  24. },
  25. getTotalDay(year, month) {
  26. const list = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  27. if ((!(year % 4) && (year % 100) || !(year % 400))) {
  28. // 闰年
  29. list[2] = 29;
  30. }
  31. return list[month];
  32. },

获取下一天

  1. tempToday = new Date();
  2. tempDate = new Date(tempToday.setDate(tempToday.getDate() + i));