当前位置:   article > 正文

微信小程序--多种类型日期选择器(年月、月日...)_微信小程序时间筛选

微信小程序时间筛选

这里写自定义目录标题
微信小程序--多种类型日期选择器(年月、月日...)
时间选择器(年月日)
时间选择器(年月)
时间选择器(年)
时间选择器(月日)
时间选择器(月)
时间选择器(日)
微信小程序–多种类型日期选择器(年月、月日…)
在业务开发的过程中,有许多对picker选择器的使用场景,根据开发需要列出了几种常见的类型,可供大家共同参考、学习

时间选择器(年月日)
效果图:

wxml:

  1. <picker mode="date" header-text="选择时间" value="{{date}}" bindchange="bindDateChange">
  2. <view class="font30">
  3. <text class="color3">填报时间:</text>
  4. <text class="color6">{{date}}</text>
  5. <text class="timeIcon"></text>
  6. </view>
  7. </picker>

js

  1. data:{
  2. date:'2021-01-01',
  3. },
  4. bindDateChange: function(e) {
  5. console.log('picker发送选择改变,携带值为', e.detail.value)
  6. this.setData({
  7. date: e.detail.value
  8. })
  9. },

wxss

  1. .font30{
  2. font-size: 30rpx;
  3. }
  4. .color3{
  5. color: #333;
  6. }
  7. .color6{
  8. color: #666;
  9. }
  10. .timeIcon{
  11. color:#666;
  12. font-size:18rpx;
  13. margin-left: 5rpx;
  14. }

时间选择器(年月)

效果图:

wxml:

  1. <picker mode="date" header-text="选择时间" fields="month" value="{{date}}" bindchange="bindDateChange">
  2. <view class="font30">
  3. <text class="color3">填报时间:</text>
  4. <text class="color6">{{date}}</text>
  5. <text class="timeIcon"></text>
  6. </view>
  7. </picker>

js

  1. data:{
  2. date:'2021-01',
  3. },
  4. bindDateChange: function(e) {
  5. console.log('picker发送选择改变,携带值为', e.detail.value)
  6. this.setData({
  7. date: e.detail.value
  8. })
  9. },

wxss

  1. .font30{
  2. font-size: 30rpx;
  3. }
  4. .color3{
  5. color: #333;
  6. }
  7. .color6{
  8. color: #666;
  9. }
  10. .timeIcon{
  11. color:#666;
  12. font-size:18rpx;
  13. margin-left: 5rpx;
  14. }

时间选择器(年)

wxml:

  1. <picker mode="date" header-text="选择时间" fields="year" value="{{date}}" bindchange="bindDateChange">
  2. <view class="font30">
  3. <text class="color3">填报时间:</text>
  4. <text class="color6">{{date}}</text>
  5. <text class="timeIcon"></text>
  6. </view>
  7. </picker>

js

  1. data:{
  2. date:'2021',
  3. },
  4. bindDateChange: function(e) {
  5. console.log('picker发送选择改变,携带值为', e.detail.value)
  6. this.setData({
  7. date: e.detail.value
  8. })
  9. },

wxss

  1. .font30{
  2. font-size: 30rpx;
  3. }
  4. .color3{
  5. color: #333;
  6. }
  7. .color6{
  8. color: #666;
  9. }
  10. .timeIcon{
  11. color:#666;
  12. font-size:18rpx;
  13. margin-left: 5rpx;
  14. }

时间选择器(月日)

(转载文章 如有雷同 纯属巧合)

这个官方没有给案例 只能自己自定义一个component组件(datePicker)

datePicker.wxml:

  1. <view class='modal-mask' wx:if="{{ showDatePickerPlus }}" bindtap='closeDatePickerPlus'></view>
  2. <view class='datepicker' wx:if="{{ showDatePickerPlus }}" animation="{{animationData}}">
  3. <view class='datepicker-header'>
  4. <view class='datepicker-header-left' bindtap='closeDatePickerPlus'>取消</view>
  5. <view class='datepicker-header-right' bindtap='submitSelectDate'>确定</view>
  6. </view>
  7. <view class='datepicker-content'>
  8. <picker-view value="{{glDateNoYearMutiIndex}}" bindchange="bindDateGlNoYear">
  9. <picker-view-column class="picker-view-column">
  10. <view wx:for="{{glMonthsNoYear}}" wx:key="index">{{item}}月</view>
  11. </picker-view-column>
  12. <picker-view-column class="picker-view-column">
  13. <view wx:for="{{glDaysNoYear}}" wx:key="index">{{item}}日</view>
  14. </picker-view-column>
  15. </picker-view>
  16. </view>
  17. </view>

datePicker.js:

  1. // component/DatePicker/DatePicker.js
  2. // 公历日期 1900 - 今天
  3. const glYear = [];
  4. for (let i = 1900; i <= 2049; i++) {
  5. glYear.push(i)
  6. }
  7. // 公历月份
  8. const glMonth = [];
  9. for (let i = 1; i <= 12; i++) {
  10. glMonth.push(i < 10 ? ('0' + i) : i)
  11. }
  12. // 公历天份 28
  13. const glDay28 = [];
  14. for (let i = 1; i <= 28; i++) {
  15. glDay28.push(i < 10 ? ('0' + i) : i)
  16. }
  17. // 公历天份 29
  18. const glDay29 = [];
  19. for (let i = 1; i <= 29; i++) {
  20. glDay29.push(i < 10 ? ('0' + i) : i)
  21. }
  22. // 公历天份 30
  23. const glDay30 = [];
  24. for (let i = 1; i <= 30; i++) {
  25. glDay30.push(i < 10 ? ('0' + i) : i)
  26. }
  27. // 公历天份 31
  28. const glDay31 = [];
  29. for (let i = 1; i <= 31; i++) {
  30. glDay31.push(i < 10 ? ('0' + i) : i)
  31. }
  32. /**
  33. * 公历年月日处理
  34. */
  35. function glDateHandler(arr) {
  36. let indexArr = arr
  37. let data = {
  38. "yearCol": glYear,
  39. "monthCol": glMonth,
  40. "dayCol": []
  41. }
  42. if (arr[1] == 0 || arr[1] == 2 || arr[1] == 4 || arr[1] == 6 || arr[1] == 7 || arr[1] == 9 || arr[1] == 11)
  43. data.dayCol = glDay31
  44. else if (arr[1] != 1)
  45. data.dayCol = glDay30
  46. else {
  47. let year = glYear[arr[0]];
  48. data.dayCol = glDay28;
  49. // 判断闰年
  50. if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
  51. data.dayCol = glDay29;
  52. }
  53. }
  54. return data;
  55. }
  56. /**
  57. * 公历月日处理 不带年份
  58. */
  59. function glDateHandlerNoYear(arr) {
  60. let indexArr = arr
  61. let data = {
  62. "monthCol": [],
  63. "dayCol": []
  64. }
  65. data.monthCol = glMonth
  66. if (arr[0] == 0 || arr[0] == 2 || arr[0] == 4 || arr[0] == 6 || arr[0] == 7 || arr[0] == 9 || arr[0] == 11)
  67. data.dayCol = glDay31
  68. else if (arr[0] != 1)
  69. data.dayCol = glDay30
  70. else
  71. data.dayCol = glDay29
  72. return data;
  73. }
  74. const formatTime = date => {
  75. const year = date.getFullYear()
  76. const month = date.getMonth() + 1
  77. const day = date.getDate()
  78. return [year, month, day].map(formatNumber).join('-')
  79. }
  80. const formatNumber = n => {
  81. n = n.toString()
  82. return n[1] ? n : '0' + n
  83. }
  84. Component({
  85. behaviors: [],
  86. properties: {
  87. // 控制是否显示日期选择组件的flag
  88. showDatePickerPlus: {
  89. type: Boolean, // 类型(必填),目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型)
  90. value: false, // 属性初始值(可选),如果未指定则会根据类型选择一个
  91. // 属性被改变时执行的函数(可选),也可以写成在methods段中定义的方法名字符串, 如:'_propertyChange'
  92. observer: function (newVal, oldVal) {
  93. if (newVal != oldVal) {
  94. if (newVal)
  95. this.showDatePickerPlus()
  96. else
  97. this.closeDatePickerPlus()
  98. }
  99. }
  100. },
  101. initDate: {
  102. type: String,
  103. value: formatTime(new Date())
  104. }
  105. },
  106. // 私有数据,可用于模版渲染
  107. data: {
  108. glDateMutiIndex: [90, 0, 0],
  109. glDateNoYearMutiIndex: [0, 0],
  110. glYears: [],
  111. glMonths: [],
  112. glMonthsNoYear: [],
  113. glDays: [],
  114. glDaysNoYear: [],
  115. animationData: {},// 动画
  116. showDatePickerPlus: false,
  117. },
  118. // 生命周期函数,可以为函数,或一个在methods段中定义的方法名
  119. attached: function () {
  120. /**
  121. * 初始化阴阳历数据
  122. */
  123. let glDateMutiIndex = this.data.glDateMutiIndex;
  124. let glDateNoYearMutiIndex = this.data.glDateNoYearMutiIndex;
  125. let [y,m,d] = this.data.initDate.split('-');
  126. glDateMutiIndex = [glYear.indexOf(parseInt(y)), parseInt(m) - 1, parseInt(d) - 1];
  127. let glDate = glDateHandler(glDateMutiIndex);
  128. glDateNoYearMutiIndex = [parseInt(m) - 1, parseInt(d) - 1];
  129. let glDateNoYear = glDateHandlerNoYear(glDateNoYearMutiIndex);
  130. this.setData({
  131. glDateMutiIndex: glDateMutiIndex,
  132. glDateNoYearMutiIndex: glDateNoYearMutiIndex,
  133. glYears: glDate.yearCol,
  134. glMonths: glDate.monthCol,
  135. glDays: glDate.dayCol,
  136. glMonthsNoYear: glDateNoYear.monthCol,
  137. glDaysNoYear: glDateNoYear.dayCol,
  138. })
  139. },
  140. moved: function () {
  141. },
  142. detached: function () {
  143. },
  144. methods: {
  145. /**
  146. * 公历数据改变监听
  147. */
  148. bindDateGl: function (e) {
  149. let value = e.detail.value;
  150. let data = glDateHandler(value);
  151. this.setData({
  152. glDateMutiIndex: value,
  153. glYears: data.yearCol,
  154. glMonths: data.monthCol,
  155. glDays: data.dayCol,
  156. })
  157. },
  158. /**
  159. * 公历不带年份数据改变监听
  160. */
  161. bindDateGlNoYear: function (e) {
  162. let value = e.detail.value;
  163. let data = glDateHandlerNoYear(value);
  164. this.setData({
  165. glDateNoYearMutiIndex: value,
  166. glMonthsNoYear: data.monthCol,
  167. glDaysNoYear: data.dayCol
  168. })
  169. },
  170. /**
  171. * 选择日期->确定
  172. */
  173. submitSelectDate: function () {
  174. let date = '';
  175. date = (this.data.glMonths[this.data.glDateNoYearMutiIndex[0]] + '-' + this.data.glDaysNoYear[this.data.glDateNoYearMutiIndex[1]])
  176. this.closeDatePickerPlus();
  177. var myEventDetail = {
  178. dateStr: date,
  179. } // detail对象,提供给事件监听函数
  180. var myEventOption = {} // 触发事件的选项
  181. this.triggerEvent('submit', myEventDetail, myEventOption)
  182. },
  183. // 显示日期选择控件
  184. showDatePickerPlus: function () {
  185. var animation = wx.createAnimation({
  186. duration: 160,
  187. timingFunction: 'linear',
  188. delay: 0
  189. })
  190. this.animation = animation
  191. animation.translateY(280).step({duration: 0})
  192. this.setData({
  193. animationData: animation.export(),
  194. showDatePickerPlus: true
  195. })
  196. setTimeout(function () {
  197. animation.translateY(0).step()
  198. this.setData({
  199. animationData: animation.export(),
  200. })
  201. }.bind(this), 160)
  202. },
  203. // 隐藏日期选择控件
  204. closeDatePickerPlus: function () {
  205. this.animation.translateY(280).step()
  206. this.setData({
  207. animationData: this.animation.export(),
  208. })
  209. setTimeout(function () {
  210. this.setData({
  211. showDatePickerPlus: false,
  212. })
  213. }.bind(this), 120)
  214. },
  215. onMyButtonTap: function () {
  216. this.setData({
  217. // 更新属性和数据的方法与更新页面数据的方法类似
  218. })
  219. },
  220. _propertyChange: function (newVal, oldVal) {
  221. }
  222. }
  223. })

datePicker.JSON:

  1. {
  2. "component": true,
  3. "usingComponents": {}
  4. }
  1. /* component/DatePicker/DatePicker.wxss */
  2. /* 日期选择器 */
  3. .modal-mask {
  4. width: 100%;
  5. height: 100%;
  6. position: fixed;
  7. top: 0;
  8. left: 0;
  9. background: #000;
  10. opacity: 0.3;
  11. overflow: hidden;
  12. z-index: 500;
  13. color: #fff;
  14. }
  15. .datepicker {
  16. position: fixed;
  17. bottom: 0;
  18. left: 0;
  19. width: 100%;
  20. background: #fff;
  21. z-index: 501;
  22. }
  23. .datepicker-header {
  24. display: flex;
  25. height: 80rpx;
  26. background-color: #e9e9e9;
  27. color: #2f80f2;
  28. align-items: center;
  29. justify-content: space-between;
  30. font-size: 28rpx;
  31. }
  32. .datepicker-header-left {
  33. height: 100%;
  34. margin-left: 30rpx;
  35. display: flex;
  36. align-items: center;
  37. }
  38. .datepicker-header-right {
  39. margin-right: 40rpx;
  40. }
  41. .datetype-toggle {
  42. background: #fff;
  43. border: 1rpx solid #2f80f2;
  44. display: flex;
  45. margin-right: 50rpx;
  46. }
  47. .datetype-item {
  48. width: 80rpx;
  49. height: 50rpx;
  50. display: flex;
  51. align-items: center;
  52. justify-content: center;
  53. }
  54. .datetype-select {
  55. background: #2f80f2;
  56. color: #fff;
  57. }
  58. .datepicker-content {
  59. height: 100%;
  60. }
  61. picker-view {
  62. width: 100%;
  63. height: 480rpx;
  64. }
  65. .picker-view-column {
  66. text-align:center;
  67. line-height: 80rpx;
  68. color: #222;
  69. font-size: 28rpx;
  70. }

index.wxml

  1. <view class="font30 holidaysTime" bindtap='showDatePickerPlus'>
  2. <text class="color3">选择时间:</text>
  3. <text class="color6">{{dateInfo.dateStr}}</text>
  4. <text class="timeIcon"></text>
  5. <datepicker-plus bind:submit='submit' showDatePickerPlus='{{show}}' initDate="2018-01-01"></datepicker-plus>
  6. </view>

index.js

  1. data:{
  2. show: false,
  3. dateInfo: {dateStr:'01-01'}
  4. },
  5. submit: function(e) {
  6. console.log(e);
  7. console.log('日期字符串:' + e.detail.dateStr)
  8. this.setData({
  9. dateInfo: e.detail
  10. })
  11. console.log('dateInfo',e.detail);
  12. },
  13. showDatePickerPlus: function() {
  14. this.setData({
  15. show: true
  16. })
  17. },

index.JSON

  1. "usingComponents": {
  2. "datepicker-plus": "/component/DatePicker/DatePicker"
  3. },

index.WXSS

  1. .holidaysTime{
  2. margin-bottom: 60rpx;
  3. }
  4. .font30{
  5. font-size: 30rpx;
  6. }
  7. .color3{
  8. color: #333;
  9. }
  10. .color6{
  11. color: #666;
  12. }
  13. .timeIcon{
  14. color:#666;
  15. font-size:18rpx;
  16. margin-left: 5rpx;
  17. }

时间选择器(月)

效果图:

wxml:

  1. <picker bindchange="bindPickerdateChange" value="{{index}}" range="{{monthArr}}">
  2. <view class="font30">
  3. <text class="color3 margin10">月份</text>
  4. <text class="color6">{{monthArr[index]}}</text>
  5. <text class="color6 timeIcon"></text>
  6. </view>
  7. </picker>

js

  1. data:{
  2. index:0,
  3. monthArr:['01月','02月','03月','04月','05月','06月','07月','08月','09月','10月','11月','12月'],
  4. },
  5. bindDateChange: function(e) {
  6. console.log('picker发送选择改变,携带值为', e.detail.value)
  7. this.setData({
  8. date: e.detail.value
  9. })
  10. },

wxss

  1. .font30{
  2. font-size: 30rpx;
  3. }
  4. .color3{
  5. color: #333;
  6. }
  7. .color6{
  8. color: #666;
  9. }
  10. .timeIcon{
  11. color:#666;
  12. font-size:18rpx;
  13. margin-left: 5rpx;
  14. }

时间选择器(日)

效果图:

wxml:

  1. <picker bindchange="bindPickerdateChange" value="{{index}}" range="{{dayArr}}">
  2. <view class="font30">
  3. <text class="color3 margin10">日期</text>
  4. <text class="color6">{{dayArr[index]}}</text>
  5. <text class="color6 timeIcon"></text>
  6. </view>
  7. </picker>

js

  1. data:{
  2. index:0,
  3. dayArr:['01日','02日','03日','04日','05日','06日','07日','08日','09日','10日','11日','12日','13日','14日','15日','16日','17日','18日','19日','20日','21日','22日','23日','24日','25日','26日','27日','28日','29日','20日','21日'],
  4. },
  5. bindPickerdateChange: function(e) {
  6. console.log('picker发送选择改变,携带值为', e.detail.value)
  7. this.setData({
  8. index: e.detail.value
  9. })
  10. },

wxss

  1. .font30{
  2. font-size: 30rpx;
  3. }
  4. .color3{
  5. color: #333;
  6. }
  7. .color6{
  8. color: #666;
  9. }
  10. .timeIcon{
  11. color:#666;
  12. font-size:18rpx;
  13. margin-left: 5rpx;
  14. }

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

闽ICP备14008679号