You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.6 KiB

5 months ago
  1. import dayjs from 'dayjs/esm/index'
  2. export default {
  3. methods: {
  4. // 设置月份数据
  5. setMonth() {
  6. // 月初是周几
  7. const day = dayjs(this.date).date(1).day()
  8. const start = day == 0 ? 6 : day - 1
  9. // 本月天数
  10. const days = dayjs(this.date).endOf('month').format('D')
  11. // 上个月天数
  12. const prevDays = dayjs(this.date).endOf('month').subtract(1, 'month').format('D')
  13. // 日期数据
  14. const arr = []
  15. // 清空表格
  16. this.month = []
  17. // 添加上月数据
  18. arr.push(
  19. ...new Array(start).fill(1).map((e, i) => {
  20. const day = prevDays - start + i + 1
  21. return {
  22. value: day,
  23. disabled: true,
  24. date: dayjs(this.date).subtract(1, 'month').date(day).format('YYYY-MM-DD')
  25. }
  26. })
  27. )
  28. // 添加本月数据
  29. arr.push(
  30. ...new Array(days - 0).fill(1).map((e, i) => {
  31. const day = i + 1
  32. return {
  33. value: day,
  34. date: dayjs(this.date).date(day).format('YYYY-MM-DD')
  35. }
  36. })
  37. )
  38. // 添加下个月
  39. arr.push(
  40. ...new Array(42 - days - start).fill(1).map((e, i) => {
  41. const day = i + 1
  42. return {
  43. value: day,
  44. disabled: true,
  45. date: dayjs(this.date).add(1, 'month').date(day).format('YYYY-MM-DD')
  46. }
  47. })
  48. )
  49. // 分割数组
  50. for (let n = 0; n < arr.length; n += 7) {
  51. this.month.push(
  52. arr.slice(n, n + 7).map((e, i) => {
  53. e.index = i + n
  54. // 自定义信息
  55. const custom = this.customList.find((c) => c.date == e.date)
  56. // 农历
  57. if (this.lunar) {
  58. const {
  59. IDayCn,
  60. IMonthCn
  61. } = this.getLunar(e.date)
  62. e.lunar = IDayCn == '初一' ? IMonthCn : IDayCn
  63. }
  64. return {
  65. ...e,
  66. ...custom
  67. }
  68. })
  69. )
  70. }
  71. }
  72. }
  73. }