当前位置:   article > 正文

uniapp自定义时间选择器_uniapp自定义选择时间

uniapp自定义选择时间

uniapp的组件进行的重新编写,可直接使用。

功能主要效果为,不可以选择当前日期之前的日期

html部分代码

  1. <view class="content">
  2. <button @click="getuser"> 获取</button>
  3. <view class="uni-title">日期:{{year}}年{{month}}月{{day}}日</view>
  4. <picker-view v-if="visible" :indicator-style="indicatorStyle" :value="value" @change="bindChange" class="picker-view">
  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>

js部分代码,其中可以在这基础上去更改限制的范围,只需要更改对应年份或月份的for循环中 i 的范围即可。

  1. export default {
  2. data() {
  3. const date = new Date()
  4. const years = []
  5. const year = date.getFullYear()
  6. const months = []
  7. const month = date.getMonth() + 1
  8. const days = []
  9. const day = date.getDate()
  10. var lastDate = new Date(year, month, 0).getDate();//每个月最后一天
  11. for (let i = year; i <= year+4; i++) {
  12. years.push(i)
  13. }
  14. for (let i = month; i <= 12; i++) {
  15. months.push(i)
  16. }
  17. for (let i = day; i <= lastDate; i++) {
  18. days.push(i)
  19. }
  20. return {
  21. title: 'picker-view',
  22. visible: false,
  23. indicatorStyle: `height: 50px;`,
  24. years,
  25. year,
  26. months,
  27. month,
  28. days,
  29. day,
  30. value: [0,0,0],
  31. }
  32. },
  33. methods: {
  34. getuser(){
  35. this.visible=true
  36. },
  37. bindChange: function (e) {
  38. const val = e.detail.value
  39. const year=new Date().getFullYear()
  40. const month=new Date().getMonth()+1
  41. const day = new Date().getDate()
  42. // 非当前年
  43. if(this.years[val[0]]>year){
  44. this.months=[]
  45. for (let i = 1; i <= 12; i++) {
  46. this.months.push(i)
  47. }
  48. var lastDate = new Date(this.years[val[0]], this.months[val[1]], 0).getDate();
  49. this.days=[]
  50. for (let i =1; i <= lastDate; i++) {
  51. this.days.push(i)
  52. }
  53. this.year = this.years[val[0]]
  54. this.month = this.months[val[1]]
  55. this.day = this.days[val[2]]
  56. }else if(this.years[val[0]]==year)
  57. //当前年
  58. {
  59. // 月份限制
  60. this.months=[]
  61. for (let i =month; i <= 12; i++) {
  62. this.months.push(i)
  63. }
  64. //获取月份的最后一天
  65. var lastDate = new Date(year, this.months[val[1]], 0).getDate();
  66. //当前月的剩余日期
  67. if(this.months[val[1]]== month){
  68. this.days=[]
  69. for (let i =day; i <= lastDate; i++) {
  70. this.days.push(i)
  71. }
  72. }else if(this.months[val[1]]>month)
  73. //非当前月的剩余日期
  74. {
  75. this.days=[]
  76. for (let i =1; i <= lastDate; i++) {
  77. this.days.push(i)
  78. }
  79. }
  80. this.year = year
  81. this.month = this.months[val[1]]
  82. this.day = this.days[val[2]]
  83. }
  84. }
  85. }
  86. }

css部分

.picker-view {
            width: 750rpx;
            height: 600rpx;
            margin-top: 20rpx;
        }
        .item {
            line-height: 100rpx;
            text-align: center;
        }

 

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

闽ICP备14008679号