slambb-picker-date.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <view class="date-container ">
  3. <view class="date-view">
  4. <picker-view class="mpvue-picker-view" v-if="visible" :indicator-style="indicatorStyle" :value="value" @change="bindChange">
  5. <picker-view-column>
  6. <view class="item" v-for="(item,index) in years" :key="index">{{item}}</view>
  7. </picker-view-column>
  8. <picker-view-column>
  9. <view class="item" v-for="(item,index) in months" :key="index">{{item}}</view>
  10. </picker-view-column>
  11. <picker-view-column>
  12. <view class="item" v-for="(item,index) in days" :key="index">{{item}}</view>
  13. </picker-view-column>
  14. </picker-view>
  15. </view>
  16. <view class="picker-unit text-gray">年 月 日</view>
  17. <!-- <view class="picker-unit-center">年月日 </view> -->
  18. </view>
  19. </template>
  20. <script>
  21. var minDate = 1950;
  22. export default {
  23. data() {
  24. const date = new Date()
  25. const years = []
  26. const year = date.getFullYear()
  27. const months = []
  28. const month = date.getMonth() + 1
  29. const days = []
  30. const day = date.getDate()
  31. // for (let i = 2040; i >= 1950; i--) {
  32. // years.push(i)
  33. // }
  34. // for (let i = 12; i > 0; i--) {
  35. // months.push(i)
  36. // }
  37. // for (let i = 31; i > 0; i--) {
  38. // days.push(i)
  39. // }
  40. for (let i = minDate; i <= date.getFullYear()+10; i++) {
  41. years.push(i)
  42. }
  43. for (let i = 1; i <= 12; i++) {
  44. months.push(i)
  45. }
  46. for (let i = 1; i <= 31; i++) {
  47. days.push(i)
  48. }
  49. return {
  50. // 年月日
  51. years,
  52. year,
  53. months,
  54. month,
  55. days,
  56. day,
  57. value: [year-minDate, month - 1, day - 1],
  58. // value: [2040 - year, 12 - (month - 1), 30 - (day - 1)],
  59. visible: true,
  60. indicatorStyle: `height: 65rpx; `
  61. }
  62. },
  63. mounted() {
  64. // console.log('显示时候更新一次数据');
  65. this.value=[this.year-minDate, this.month - 1, this.day - 1];
  66. this.$emit('bindChangeDate', [this.year, this.month, this.day]);
  67. },
  68. methods: {
  69. bindChange: function(e) {
  70. const val = e.detail.value
  71. this.year = this.years[val[0]]
  72. this.month = this.months[val[1]]
  73. this.day = this.days[val[2]]
  74. //将选择的年月日变为number形式,便于比较之用
  75. var y = parseInt(this.year)
  76. var m = parseInt(this.month)
  77. var d = parseInt(this.day)
  78. //选择不同月份显示的天数不同
  79. if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
  80. if (this.days.length != 31) {
  81. this.days = []
  82. for (let i = 1; i <= 31; i++) {
  83. this.days.push(i)
  84. }
  85. // for (let i = 31; i > 0; i--) {
  86. // this.days.push(i)
  87. // }
  88. }
  89. } else if (m == 4 || m == 6 || m == 9 || m == 11) {
  90. if (this.days.length != 30) {
  91. this.days = []
  92. for (let i = 1; i <= 30; i++) {
  93. this.days.push(i)
  94. }
  95. // for (let i = 30; i > 0; i--) {
  96. // this.days.push(i)
  97. // }
  98. }
  99. } else if (m == 2) {
  100. if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)) { //闰年
  101. if (this.days.length != 29) {
  102. this.days = []
  103. for (let i = 1; i <= 29; i++) {
  104. this.days.push(i)
  105. }
  106. // for (let i = 29; i > 0; i--) {
  107. // this.days.push(i)
  108. // }
  109. }
  110. } else { //平年
  111. if (this.days.length != 28) {
  112. this.days = []
  113. for (let i = 1; i <= 28; i++) {
  114. this.days.push(i)
  115. }
  116. // for (let i = 28; i > 0; i--) {
  117. // this.days.push(i)
  118. // }
  119. }
  120. }
  121. }
  122. //处理选择今年的情况
  123. if (y == this.currentYear) {
  124. //最多显示到当前月份
  125. if (this.months.length != this.currentMonth) {
  126. this.months = []
  127. for (let i = 1; i <= this.currentMonth; i++) {
  128. this.months.push(i)
  129. }
  130. // for (let i = this.currentMonth; i > 0; i--) {
  131. // this.months.push(i)
  132. // }
  133. }
  134. //如果选择的是当前月份,那么日最多显示到今天
  135. if (m == this.currentMonth) {
  136. if (this.days.length != this.currentDay) {
  137. this.days = []
  138. for (let i = 1; i <= this.currentDay; i++) {
  139. this.days.push(i)
  140. }
  141. // for (let i = this.currentDay; i > 0; i--) {
  142. // this.days.push(i)
  143. // }
  144. }
  145. }
  146. } else {
  147. this.months = []
  148. for (let i = 1; i <= 12; i++) {
  149. this.months.push(i)
  150. }
  151. // for (let i = 12; i > 0; i--) {
  152. // this.months.push(i)
  153. // }
  154. }
  155. this.$emit('bindChangeDate', [this.year, this.month, this.day])
  156. },
  157. }
  158. }
  159. </script>
  160. <style>
  161. /* picker */
  162. .date-container {
  163. position: relative;
  164. display: flex;
  165. justify-content: center;
  166. }
  167. .date-view {
  168. background: #FFFFFF;
  169. width: 100%;
  170. height: 300rpx;
  171. display: flex;
  172. justify-content: center;
  173. border-radius: 20upx;
  174. }
  175. .mpvue-picker-view {
  176. width: calc(80% - 200rpx);
  177. /* padding-top: 10px; */
  178. height: 300rpx;
  179. /* background-color: rgba(255, 85, 0, 1); */
  180. border-radius: 20px;
  181. /* display: flex; */
  182. }
  183. .item {
  184. text-align: center;
  185. width: calc(100% - 0px);
  186. /* background-color: rgba(255, 170, 127, 1); */
  187. height: 65rpx;
  188. line-height: 65rpx;
  189. text-overflow: ellipsis;
  190. white-space: nowrap;
  191. font-size: 20px;
  192. color: rgba(164, 136, 220, 1);
  193. /* padding-right: 10upx; */
  194. /* border: 1rpx solid #007AFF; */
  195. }
  196. .picker-unit {
  197. position: absolute;
  198. left: 20px;
  199. top: 20px;
  200. font-size: 13px;
  201. line-height: 26px;
  202. z-index: 100;
  203. }
  204. .picker-unit-center {
  205. position: absolute;
  206. left: 90rpx;
  207. top: 120rpx;
  208. width: 100%;
  209. font-size: 13px;
  210. color: rgba(164, 136, 220, 1);
  211. line-height: 40px;
  212. z-index: 100;
  213. letter-spacing: 80rpx;
  214. }
  215. </style>