picker.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. Page({
  2. /**
  3. * 页面的初始数据
  4. */
  5. data: {
  6. years: 0,
  7. months: 0,
  8. days: 0,
  9. value: [8, 1, 1],//默认滚动的索引值
  10. },
  11. /**
  12. * 生命周期函数--监听页面加载
  13. */
  14. onLoad: function (options) {
  15. },
  16. /**
  17. * 生命周期函数--监听页面初次渲染完成
  18. */
  19. onReady: function () {
  20. },
  21. /**
  22. * 生命周期函数--监听页面显示
  23. */
  24. onShow: function () {
  25. },
  26. /**
  27. * 生命周期函数--监听页面隐藏
  28. */
  29. onHide: function () {
  30. },
  31. /**
  32. * 生命周期函数--监听页面卸载
  33. */
  34. onUnload: function () {
  35. },
  36. /**
  37. * 页面相关事件处理函数--监听用户下拉动作
  38. */
  39. onPullDownRefresh: function () {
  40. },
  41. /**
  42. * 页面上拉触底事件的处理函数
  43. */
  44. onReachBottom: function () {
  45. },
  46. /**
  47. * 用户点击右上角分享
  48. */
  49. onShareAppMessage: function () {
  50. },
  51. bindDateChangeStart(e) {
  52. // valIndex 是获取到的年月日在各自集合中的下标
  53. const valIndex = e.mp.detail.value
  54. // console.log(JSON.stringify(e.mp.detail.value))
  55. let year = this.years[valIndex[0]]
  56. let month = this.months[valIndex[1]]
  57. let day = this.days[valIndex[2]]
  58. // 滚动时再动态 通过年和月获取 这个月下对应有多少天
  59. this.getMonth(year, month, day)
  60. },
  61. initDatas() {
  62. const date = new Date()
  63. const nowYear = date.getFullYear()
  64. const nowMonth = date.getMonth() + 1
  65. const nowDay = date.getDate()
  66. this.year = nowYear
  67. this.month = nowMonth
  68. this.day = nowDay
  69. // 循环前先清空数组
  70. this.years = []
  71. this.months = []
  72. for (let i = nowYear - 30; i <= nowYear; i++) {
  73. this.years.push(i)
  74. }
  75. // 设置月份列表
  76. for (let i = 1; i <= 12; i++) {
  77. this.months.push(i)
  78. }
  79. // 初始化当前年月
  80. if (this.birthday && this.birthday.indexOf('-') != -1) {
  81. var birthday_obj = this.birthday.split('-');
  82. this.getMonth(parseInt(birthday_obj[0]), parseInt(birthday_obj[1]), parseInt(birthday_obj[2]))
  83. } else {
  84. this.getMonth(nowYear, nowMonth, nowDay)
  85. }
  86. },
  87. getMonth(year, month, day) {
  88. let daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  89. let dayNum = 0
  90. // 通过年和月获取这个月份下有多少天
  91. if (month === 2) { // 闰年
  92. dayNum = ((year % 4 === 0) && ((year % 100) !== 0)) || (year % 400 === 0) ? 29 : 28
  93. } else {
  94. dayNum = daysInMonth[month - 1]
  95. }
  96. this.days = []
  97. for (let i = 1; i <= dayNum; i++) {
  98. this.days.push(i)
  99. }
  100. // 初始 选中年月日对应下标
  101. let yearIdx = 0
  102. let monthIdx = 0
  103. let dayIdx = 0
  104. // 获取滚动后 年月日对应的下标
  105. this.years.map((v, idx) => {
  106. if (v === year) {
  107. yearIdx = idx
  108. }
  109. })
  110. this.months.map((v, idx) => {
  111. if (v === month) {
  112. monthIdx = idx
  113. }
  114. })
  115. this.days.map((v, idx) => {
  116. if (v === day) {
  117. dayIdx = idx
  118. }
  119. })
  120. // 重置滚动后 年月日 的下标
  121. this.value = [yearIdx, monthIdx, dayIdx]
  122. // 赋值年月日
  123. this.year = this.years[yearIdx]
  124. this.month = this.months[monthIdx] > 9 ? this.months[monthIdx] : '0' + this.months[monthIdx]
  125. this.day = this.days[dayIdx] > 9 ? this.days[dayIdx] : '0' + this.days[dayIdx]
  126. },
  127. })