selectMonth.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <view class="uni-calendar__header">
  3. <view class="uni-calendar__header-btn-box" @click.stop="pre">
  4. <view
  5. class="uni-calendar__header-btn uni-calendar--left"
  6. ></view>
  7. </view>
  8. <picker
  9. mode="date"
  10. :value="selectDate"
  11. fields="month"
  12. @change="bindDateChange"
  13. >
  14. <text class="uni-calendar__header-text">
  15. {{ (nowDate.year || '') + ' / ' + (nowDate.month || '') }}
  16. </text>
  17. </picker>
  18. <view class="uni-calendar__header-btn-box" @click.stop="next">
  19. <view
  20. class="uni-calendar__header-btn uni-calendar--right"
  21. ></view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. name: 'selectMonth',
  28. data() {
  29. return {
  30. selectDate: '', // yyyy-mm
  31. date: '',
  32. nowDate: {
  33. year: '',
  34. month: '',
  35. },
  36. };
  37. },
  38. created() {
  39. const _date = new Date();
  40. const ym = uni.$u.timeFormat(_date, 'yyyy-mm');
  41. this.selectDate = ym; // 当前选中的月份
  42. const [year, month] = ym.split('-');
  43. this.nowDate.year = year;
  44. this.nowDate.month = month;
  45. this.$emit('dateChange', this.selectDate);
  46. },
  47. methods: {
  48. bindDateChange(e) {
  49. this.selectDate = e.detail.value;
  50. const [year, month] = this.selectDate.split('-');
  51. this.nowDate.year = year;
  52. this.nowDate.month = month;
  53. this.$emit('dateChange', this.selectDate);
  54. },
  55. /**
  56. * 获取任意时间
  57. */
  58. getDate(date, AddDayCount = 0, str = 'day') {
  59. if (!date) {
  60. date = new Date();
  61. }
  62. if (typeof date !== 'object') {
  63. date = date.replace(/-/g, '/');
  64. }
  65. const dd = new Date(date);
  66. switch (str) {
  67. case 'day':
  68. dd.setDate(dd.getDate() + AddDayCount); // 获取AddDayCount天后的日期
  69. break;
  70. case 'month':
  71. if (dd.getDate() === 31 && AddDayCount > 0) {
  72. dd.setDate(dd.getDate() + AddDayCount);
  73. } else {
  74. const preMonth = dd.getMonth();
  75. dd.setMonth(preMonth + AddDayCount); // 获取AddDayCount天后的日期
  76. const nextMonth = dd.getMonth();
  77. // 处理 pre 切换月份目标月份为2月没有当前日(30 31) 切换错误问题
  78. if (
  79. AddDayCount < 0 &&
  80. preMonth !== 0 &&
  81. nextMonth - preMonth > AddDayCount
  82. ) {
  83. dd.setMonth(
  84. nextMonth + (nextMonth - preMonth + AddDayCount)
  85. );
  86. }
  87. // 处理 next 切换月份目标月份为2月没有当前日(30 31) 切换错误问题
  88. if (
  89. AddDayCount > 0 &&
  90. nextMonth - preMonth > AddDayCount
  91. ) {
  92. dd.setMonth(
  93. nextMonth - (nextMonth - preMonth - AddDayCount)
  94. );
  95. }
  96. }
  97. break;
  98. case 'year':
  99. dd.setFullYear(dd.getFullYear() + AddDayCount); // 获取AddDayCount天后的日期
  100. break;
  101. }
  102. const y = dd.getFullYear();
  103. const m =
  104. dd.getMonth() + 1 < 10
  105. ? '0' + (dd.getMonth() + 1)
  106. : dd.getMonth() + 1; // 获取当前月份的日期,不足10补0
  107. const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号,不足10补0
  108. return {
  109. fullDate: y + '-' + m + '-' + d,
  110. year: y,
  111. month: m,
  112. date: d,
  113. day: dd.getDay(),
  114. };
  115. },
  116. setDate(res) {
  117. this.selectDate = `${res.year}-${res.month}`;
  118. this.nowDate.year = res.year;
  119. this.nowDate.month = res.month;
  120. this.$emit('dateChange', this.selectDate);
  121. },
  122. /**
  123. * 上个月
  124. */
  125. pre() {
  126. const res = this.getDate(this.selectDate, -1, 'month');
  127. this.setDate(res);
  128. },
  129. /**
  130. * 下个月
  131. */
  132. next() {
  133. const res = this.getDate(this.selectDate, 1, 'month');
  134. this.setDate(res);
  135. },
  136. },
  137. };
  138. </script>
  139. <style scoped lang="scss">
  140. .uni-calendar__header {
  141. position: relative;
  142. /* #ifndef APP-NVUE */
  143. display: flex;
  144. /* #endif */
  145. flex-direction: row;
  146. justify-content: center;
  147. align-items: center;
  148. height: 50px;
  149. }
  150. .uni-calendar__header-btn-box {
  151. /* #ifndef APP-NVUE */
  152. display: flex;
  153. /* #endif */
  154. flex-direction: row;
  155. align-items: center;
  156. justify-content: center;
  157. width: 50px;
  158. height: 50px;
  159. }
  160. .uni-calendar__header-btn {
  161. width: 10px;
  162. height: 10px;
  163. border-left-color: $uni-text-color-placeholder;
  164. border-left-style: solid;
  165. border-left-width: 2px;
  166. border-top-color: $uni-color-subtitle;
  167. border-top-style: solid;
  168. border-top-width: 2px;
  169. }
  170. .uni-calendar--left {
  171. transform: rotate(-45deg);
  172. }
  173. .uni-calendar--right {
  174. transform: rotate(135deg);
  175. }
  176. </style>