//该代码是基于网路代码简单修改过一点点 (等于没改) 主要用来做个笔记

    this.triggerEvent(“timePicker”, times) 用于组件传值 接收就用 bind:timePicker=”自定义方法名”

    1. <picker class="pa" mode="multiSelector" bindchange="bindMultiPickerChange" bindcolumnchange="bindMultiPickerColumnChange" value="{{multiIndex}}" range="{{multiArray}}">
    2. <input value='{{time}}' placeholder='请选择时间' />
    3. </picker>

    //js

    1. const date = new Date();
    2. const years = [];
    3. const months = [];
    4. const days = [];
    5. const hours = [];
    6. const minutes = [];
    7. //获取年
    8. for (let i = 2018; i <= date.getFullYear() + 5; i++) {
    9. years.push("" + i);
    10. }
    11. //获取月份
    12. for (let i = 1; i <= 12; i++) {
    13. if (i < 10) {
    14. i = "0" + i;
    15. }
    16. months.push("" + i);
    17. }
    18. //获取日期
    19. for (let i = 1; i <= 31; i++) {
    20. if (i < 10) {
    21. i = "0" + i;
    22. }
    23. days.push("" + i);
    24. }
    25. //获取小时
    26. for (let i = 0; i < 24; i++) {
    27. if (i < 10) {
    28. i = "0" + i;
    29. }
    30. hours.push("" + i);
    31. }
    32. //获取分钟
    33. for (let i = 0; i < 60; i++) {
    34. if (i < 10) {
    35. i = "0" + i;
    36. }
    37. minutes.push("" + i);
    38. }
    39. Page({
    40. data: {
    41. time: '',
    42. multiArray: [years, months, days, hours, minutes],
    43. multiIndex: [0, 9, 16, 10, 17],
    44. choose_year: '',
    45. },
    46. onLoad: function () {
    47. //设置默认的年份
    48. this.setData({
    49. choose_year: this.data.multiArray[0][0]
    50. })
    51. },
    52. //获取时间日期
    53. bindMultiPickerChange: function (e) {
    54. // console.log('picker发送选择改变,携带值为', e.detail.value)
    55. this.setData({
    56. multiIndex: e.detail.value
    57. })
    58. const index = this.data.multiIndex;
    59. const year = this.data.multiArray[0][index[0]];
    60. const month = this.data.multiArray[1][index[1]];
    61. const day = this.data.multiArray[2][index[2]];
    62. const hour = this.data.multiArray[3][index[3]];
    63. const minute = this.data.multiArray[4][index[4]];
    64. // console.log(`${year}-${month}-${day}-${hour}-${minute}`);
    65. let times = year + '-' + month + '-' + day + ' ' + hour + ':' + minute
    66. this.setData({
    67. time: times
    68. })
    69. this.triggerEvent("timePicker", times)
    70. // console.log(this.data.time);
    71. },
    72. //监听picker的滚动事件
    73. bindMultiPickerColumnChange: function (e) {
    74. //获取年份
    75. if (e.detail.column == 0) {
    76. let choose_year = this.data.multiArray[e.detail.column][e.detail.value];
    77. console.log(choose_year);
    78. this.setData({
    79. choose_year
    80. })
    81. }
    82. //console.log('修改的列为', e.detail.column, ',值为', e.detail.value);
    83. if (e.detail.column == 1) {
    84. let num = parseInt(this.data.multiArray[e.detail.column][e.detail.value]);
    85. let temp = [];
    86. if (num == 1 || num == 3 || num == 5 || num == 7 || num == 8 || num == 10 || num == 12) { //判断31天的月份
    87. for (let i = 1; i <= 31; i++) {
    88. if (i < 10) {
    89. i = "0" + i;
    90. }
    91. temp.push("" + i);
    92. }
    93. this.setData({
    94. ['multiArray[2]']: temp
    95. });
    96. } else if (num == 4 || num == 6 || num == 9 || num == 11) { //判断30天的月份
    97. for (let i = 1; i <= 30; i++) {
    98. if (i < 10) {
    99. i = "0" + i;
    100. }
    101. temp.push("" + i);
    102. }
    103. this.setData({
    104. ['multiArray[2]']: temp
    105. });
    106. } else if (num == 2) { //判断2月份天数
    107. let year = parseInt(this.data.choose_year);
    108. console.log(year);
    109. if (((year % 400 == 0) || (year % 100 != 0)) && (year % 4 == 0)) {
    110. for (let i = 1; i <= 29; i++) {
    111. if (i < 10) {
    112. i = "0" + i;
    113. }
    114. temp.push("" + i);
    115. }
    116. this.setData({
    117. ['multiArray[2]']: temp
    118. });
    119. } else {
    120. for (let i = 1; i <= 28; i++) {
    121. if (i < 10) {
    122. i = "0" + i;
    123. }
    124. temp.push("" + i);
    125. }
    126. this.setData({
    127. ['multiArray[2]']: temp
    128. });
    129. }
    130. }
    131. console.log(this.data.multiArray[2]);
    132. }
    133. var data = {
    134. multiArray: this.data.multiArray,
    135. multiIndex: this.data.multiIndex
    136. };
    137. data.multiIndex[e.detail.column] = e.detail.value;
    138. this.setData(data);
    139. },
    140. })

    less

    .pa{
      width: 100%;
      height: 100%;
      background: #fff;
      color: #495060;
      padding-right: 10px;
    }