generateDates.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. *此函数的作用是根据传入的一个日期,返回这一周的日期或者这一个月的日期,
  3. * 如果是月的话注意还包含上个月和下个月的日期,月的话总共数据有 6 * 7 = 42个
  4. *
  5. */
  6. /*
  7. * 时间格式化函数
  8. * 重要提示,微信小程序new Date('2020-04-16')在ios中无法获取时间对象
  9. * 解决方式: 建议将时间都格式化成'2020/04/16 00:00:00'的格式
  10. * 函数示例: formatDate(new Date(), 'YYYY/MM/dd hh:mm:ss')
  11. */
  12. export const formatDate = (date, fmt) => {
  13. if (/(y+)/.test(fmt)) {
  14. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  15. }
  16. let o = {
  17. 'M+': date.getMonth() + 1,
  18. 'd+': date.getDate(),
  19. 'h+': date.getHours(),
  20. 'm+': date.getMinutes(),
  21. 's+': date.getSeconds()
  22. }
  23. for (let k in o) {
  24. if (new RegExp(`(${k})`).test(fmt)) {
  25. let str = o[k] + ''
  26. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
  27. }
  28. }
  29. return fmt
  30. }
  31. const padLeftZero = (str) => {
  32. return ('00' + str).substr(str.length)
  33. }
  34. export const judgeType = (s) => {
  35. // 函数返回数据的具体类型
  36. return Object.prototype.toString.call(s).slice(8,-1);
  37. }
  38. export const equalDate = (d1, d2) => {
  39. let result = false;
  40. if (d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate()) {
  41. result = true;
  42. }
  43. return result;
  44. }
  45. /* 比较时间,时间格式为2020-04-04
  46. */
  47. export const dateEqual = (before, after) => {
  48. before = new Date(before.replace('-', '/').replace('-', '/'))
  49. after = new Date(after.replace('-', '/').replace('-', '/'))
  50. if (before.getTime() - after.getTime() === 0) {
  51. return true
  52. } else {
  53. return false
  54. }
  55. }
  56. export const gegerateDates = (date = new Date(), type='week') => {
  57. const result = [];
  58. if (judgeType(date) === 'Date') {
  59. // 年,月,日
  60. const y = date.getFullYear();
  61. const m = date.getMonth();
  62. const d = date.getDate();
  63. const days = new Date(y, m+1, 0).getDate();
  64. // 获取日期是星期几
  65. let weekIndex = date.getDay() === 0 ? 7 : date.getDay();
  66. if (type === 'month') {
  67. const dobj = new Date(y,m,1);
  68. weekIndex = dobj.getDay() === 0 ? 7 : dobj.getDay();
  69. }
  70. if (type === 'week') {
  71. for(let i = weekIndex - 1; i >0; i--) {
  72. const dtemp = new Date(y,m,d);
  73. dtemp.setDate(dtemp.getDate() - i);
  74. result.push({
  75. time: dtemp,
  76. show: true,
  77. fullDate: formatDate(dtemp, 'yyyy-MM-dd'),
  78. isToday: equalDate(new Date(), dtemp)
  79. })
  80. }
  81. for(let i = 0; i <= 7 - weekIndex; i++) {
  82. const dtemp = new Date(y,m,d);
  83. dtemp.setDate(dtemp.getDate() + i);
  84. result.push({
  85. time: dtemp,
  86. show: true,
  87. fullDate: formatDate(dtemp, 'yyyy-MM-dd'),
  88. isToday: equalDate(new Date(), dtemp)
  89. })
  90. }
  91. } else if (type === 'month') {
  92. // 上个月
  93. for(let i = weekIndex - 1; i > 0; i--) {
  94. const dtemp = new Date(y,m,1);
  95. dtemp.setDate(dtemp.getDate() - i);
  96. result.push({
  97. time: dtemp,
  98. show: false,
  99. fullDate: formatDate(dtemp, 'yyyy-MM-dd'),
  100. isToday: equalDate(new Date(), dtemp)
  101. });
  102. }
  103. // 这个月的日期
  104. for (let i = 0; i < days; i++) {
  105. const dtemp = new Date(y,m,1);
  106. dtemp.setDate(dtemp.getDate() + i);
  107. result.push({
  108. time: dtemp,
  109. show: true,
  110. fullDate: formatDate(dtemp, 'yyyy-MM-dd'),
  111. isToday: equalDate(new Date(), dtemp)
  112. });
  113. }
  114. const len = 42 - result.length;
  115. // 下个月的日期
  116. for (let i = 1; i <= len;i++) {
  117. const dtemp = new Date(y,m+1,0);
  118. dtemp.setDate(dtemp.getDate() + i);
  119. result.push({
  120. time: dtemp,
  121. show: false,
  122. fullDate: formatDate(dtemp, 'yyyy-MM-dd'),
  123. isToday: equalDate(new Date(), dtemp)
  124. })
  125. }
  126. }
  127. }
  128. return result;
  129. }