当前位置:   article > 正文

uniapp日期选择器(年 月 日)_uniapp 日期时间选择器

uniapp 日期时间选择器
  1. <template>
  2. <view>
  3. <view class="uni-padding-wrap" style="margin-top: 180rpx;">
  4. <view class="uni-title" @tap="date">
  5. 日期:{{nian}}年{{yue}}月{{ri}}日
  6. </view>
  7. </view>
  8. <uni-popup ref="wenzi" type="bottom" :maskClick=false>
  9. <view class="date-select">
  10. <view class="btn u-f u-f-jsb" style="display: flex;justify-content:space-between;">
  11. <view class="btn-left" @tap="concel">
  12. 取消
  13. </view>
  14. <view class="btn-right" @tap="confirm">
  15. 确定
  16. </view>
  17. </view>
  18. <picker-view class="picker-view" v-if="visible" :indicator-style="indicatorStyle" :value="value" @change="bindChange">
  19. <picker-view-column>
  20. <view class="item" v-for="(item,index) in years" :key="index">{{item}}年</view>
  21. </picker-view-column>
  22. <picker-view-column>
  23. <view class="item" v-for="(item,index) in months" :key="index">{{item}}月</view>
  24. </picker-view-column>
  25. <picker-view-column>
  26. <view class="item" v-for="(item,index) in days" :key="index">{{item}}日</view>
  27. </picker-view-column>
  28. </picker-view>
  29. </view>
  30. </uni-popup>
  31. </view>
  32. </template>
  33. <script>
  34. export default {
  35. data() {
  36. const date = new Date()
  37. const years = []
  38. const year = date.getFullYear()
  39. const months = []
  40. const month = date.getMonth() + 1
  41. const days = []
  42. const day = date.getDate()
  43. for (let i = 2020; i <= date.getFullYear(); i++) {
  44. years.push(i)
  45. }
  46. for (let i = 1; i <= 12; i++) {
  47. months.push(i)
  48. }
  49. for (let i = 1; i <= 31; i++) {
  50. days.push(i)
  51. }
  52. return {
  53. title: 'picker-view',
  54. years,
  55. year,
  56. months,
  57. month,
  58. days,
  59. day,
  60. value: [0, month - 1, day - 1],
  61. /**
  62. * 解决动态设置indicator-style不生效的问题
  63. */
  64. visible: true,
  65. indicatorStyle: `height: ${Math.round(uni.getSystemInfoSync().screenWidth/(750/100))}px;`,
  66. nian:'',
  67. yue:'',
  68. ri:''
  69. }
  70. },
  71. methods: {
  72. bindChange(e) {
  73. let a;
  74. let y;
  75. console.log(e);
  76. let val = e.detail.value;
  77. y = this.years[val[0]];
  78. a = this.months[val[1]];
  79. this.day = this.days[val[2]];
  80. this.month = a;
  81. this.year = y;
  82. this.days = [];
  83. if (a||y) {
  84. if (a == 1 || a == 3 || a == 5 || a == 7 || a == 8 || a == 10 || a == 12) {
  85. for (let i = 1; i <= 31; i++) {
  86. this.days.push(i)
  87. }
  88. } else if (a == 4 || a == 6 || a == 11 || a == 9) {
  89. for (let i = 1; i <= 30; i++) {
  90. this.days.push(i)
  91. }
  92. } else if (a == 2) {
  93. if (this.year % 4 == 0 && (this.year % 100 != 0 || this.year % 400 == 0)) {
  94. for (let i = 1; i <= 29; i++) {
  95. this.days.push(i)
  96. }
  97. } else {
  98. for (let i = 1; i <= 28; i++) {
  99. this.days.push(i)
  100. }
  101. }
  102. }
  103. }
  104. },
  105. date() {
  106. this.$refs.wenzi.open();
  107. },
  108. concel(){
  109. this.$refs.wenzi.close();
  110. },
  111. confirm(){
  112. this.nian =this.year;
  113. this.yue = this.month;
  114. this.ri=this.day;
  115. this.$refs.wenzi.close();
  116. }
  117. }
  118. }
  119. </script>
  120. <style scoped lang="scss">
  121. .date-select {
  122. width: 100%;
  123. height: 700upx;
  124. border-top-left-radius: 40upx;
  125. border-top-right-radius: 40upx;
  126. background-color: #FFFFFF;
  127. position: relative;
  128. .picker-view {
  129. background-color: #EEEEEE;
  130. position: absolute;
  131. width: 100%;
  132. height: 500upx;
  133. bottom: 20upx;
  134. .item {
  135. line-height: 100rpx;
  136. font-size: 34upx;
  137. text-align: center;
  138. }
  139. }
  140. .btn{
  141. width: 100%;
  142. height: 100upx;
  143. box-sizing: border-box;
  144. padding: 40upx 30upx;
  145. .btn-left,.btn-right{
  146. color: #FFFFFF;
  147. width: 150upx;
  148. height: 90upx;
  149. line-height: 90upx;
  150. text-align: center;
  151. border-radius: 15upx;
  152. }
  153. .btn-left{
  154. background-color: #BEBEBE;
  155. }
  156. .btn-right{
  157. background-color: #1cb419;
  158. }
  159. }
  160. }
  161. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/565678
推荐阅读
相关标签
  

闽ICP备14008679号