lj-time-util.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. export default {
  2. // 当某月的天数
  3. getDaysInOneMonth(date) {
  4. const year = date.getFullYear();
  5. const month = date.getMonth() + 1;
  6. const d = new Date(year, month, 0);
  7. return d.getDate();
  8. },
  9. // 向前空几个
  10. getMonthweek(date) {
  11. const year = date.getFullYear();
  12. const month = date.getMonth() + 1;
  13. const dateFirstOne = new Date(year + '/' + month + '/1');
  14. return this.sundayStart ?
  15. dateFirstOne.getDay() == 0 ? 7 : dateFirstOne.getDay() :
  16. dateFirstOne.getDay() == 0 ? 6 : dateFirstOne.getDay() - 1;
  17. },
  18. /**
  19. * 获取当前日期上个月或者下个月
  20. */
  21. getOtherMonth(date, str = 'nextMonth') {
  22. const timeArray = this.dateFormat(date).split('/');
  23. const year = timeArray[0];
  24. const month = timeArray[1];
  25. const day = timeArray[2];
  26. let year2 = year;
  27. let month2;
  28. if (str === 'nextMonth') {
  29. month2 = parseInt(month) + 1;
  30. if (month2 == 13) {
  31. year2 = parseInt(year2) + 1;
  32. month2 = 1;
  33. }
  34. } else {
  35. month2 = parseInt(month) - 1;
  36. if (month2 == 0) {
  37. year2 = parseInt(year2) - 1;
  38. month2 = 12;
  39. }
  40. }
  41. let day2 = day;
  42. const days2 = new Date(year2, month2, 0).getDate();
  43. if (day2 > days2) {
  44. day2 = days2;
  45. }
  46. if (month2 < 10) {
  47. month2 = '0' + month2;
  48. }
  49. if (day2 < 10) {
  50. day2 = '0' + day2;
  51. }
  52. const t2 = year2 + '/' + month2 + '/' + day2;
  53. return new Date(t2);
  54. },
  55. // 上个月末尾的一些日期
  56. getLeftArr(date) {
  57. const arr = [];
  58. const leftNum = this.getMonthweek(date);
  59. const num = this.getDaysInOneMonth(this.getOtherMonth(date, 'preMonth')) - leftNum + 1;
  60. const preDate = this.getOtherMonth(date, 'preMonth');
  61. // 上个月多少开始
  62. for (let i = 0; i < leftNum; i++) {
  63. const nowTime = preDate.getFullYear() + '/' + (preDate.getMonth() + 1) + '/' + (num + i);
  64. arr.push({
  65. id: num + i,
  66. date: nowTime,
  67. isToday: false,
  68. otherMonth: 'preMonth',
  69. });
  70. }
  71. return arr;
  72. },
  73. // 下个月末尾的一些日期
  74. getRightArr(date) {
  75. const arr = [];
  76. const nextDate = this.getOtherMonth(date, 'nextMonth');
  77. const leftLength = this.getDaysInOneMonth(date) + this.getMonthweek(date);
  78. const _length = 7 - leftLength % 7;
  79. for (let i = 0; i < _length; i++) {
  80. const nowTime = nextDate.getFullYear() + '/' + (nextDate.getMonth() + 1) + '/' + (i + 1);
  81. arr.push({
  82. id: i + 1,
  83. date: nowTime,
  84. isToday: false,
  85. otherMonth: 'nextMonth',
  86. });
  87. }
  88. return arr;
  89. },
  90. // format日期
  91. dateFormat(date) {
  92. date = typeof date === 'string' ? new Date(date.replace(/\-/g, '/')) : date;
  93. return date.getFullYear() + '/' + (date.getMonth() + 1) + '/'
  94. + date.getDate();
  95. },
  96. // 获取某月的列表不包括上月和下月
  97. getMonthListNoOther(date) {
  98. const arr = [];
  99. const num = this.getDaysInOneMonth(date);
  100. const year = date.getFullYear();
  101. const month = date.getMonth() + 1;
  102. const toDay = this.dateFormat(new Date());
  103. for (let i = 0; i < num; i++) {
  104. const nowTime = year + '/' + month + '/' + (i + 1);
  105. arr.push({
  106. id: i + 1,
  107. date: nowTime,
  108. isToday: toDay === nowTime,
  109. otherMonth: 'nowMonth',
  110. });
  111. }
  112. return arr;
  113. },
  114. // 获取某月的列表 用于渲染
  115. getMonthList(date) {
  116. return [ ...this.getLeftArr(date), ...this.getMonthListNoOther(date), ...this.getRightArr(date) ];
  117. },
  118. // 默认是周一开始
  119. sundayStart: false,
  120. };