index.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const month = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'],
  2. day = ['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', '30', '31'],
  3. getTime = require("../../utils/getTime");
  4. Component({
  5. /**
  6. * 组件的属性列表
  7. */
  8. properties: {
  9. startDate: {
  10. type: String,
  11. value: "2012-02-08"
  12. },
  13. endDate: {
  14. type: String,
  15. value: getTime.formatTime(new Date(), '-').split(' ')[0]
  16. }
  17. },
  18. /**
  19. * 组件的初始数据
  20. */
  21. data: {
  22. value: [9, 3, 3]
  23. },
  24. lifetimes: {
  25. ready() {
  26. let end = this.data.endDate.split('-')[0];
  27. let years = [];
  28. for (let start = this.data.startDate.split('-')[0] - 0; start <= end; start++) {
  29. years.push(start)
  30. };
  31. this.setData({
  32. years
  33. });
  34. this.handleChange(this.data.startDate)
  35. }
  36. },
  37. /**
  38. * 组件的方法列表
  39. */
  40. methods: {
  41. handleChange(date) {
  42. let arr = date.split('-'),
  43. start = this.data.startDate.split('-');
  44. let m = arr[1] - 0,
  45. d = arr[2] - 0;
  46. console.log(arr)
  47. this.setData({
  48. months: (arr[0] == start[0]) ? month.slice(start[1] - 1) : month
  49. })
  50. if (arr[0] == start[0]) {
  51. this.handleDays(arr[0], m, (m == start[1] - 0) ? start[2] - 0 : 0);
  52. } else {
  53. this.handleDays(arr[0], m, 0);
  54. }
  55. },
  56. handleDays(y, m, d) {
  57. let arr1 = [1, 3, 5, 7, 8, 10, 12];
  58. let days = JSON.parse(JSON.stringify(day))
  59. if (m == 2) {
  60. days = (y % 4 == 0) ? days.slice(0, 29) : days.slice(0, 28)
  61. } else {
  62. (arr1.includes(m)) ? '' : days.pop();
  63. }
  64. console.log(days)
  65. this.setData({
  66. days: (d == 0) ? days : days.slice(d - 1)
  67. })
  68. },
  69. pickerChange({
  70. detail
  71. }) {
  72. let arr = detail.value;
  73. this.handleChange(`${this.data.years[arr[0]]}-${this.data.months[arr[1]]}-${this.data.days[arr[2]]}`)
  74. setTimeout(() => {
  75. console.log(`${this.data.years[arr[0]]}-${this.data.months[arr[1]]}-${this.data.days[arr[2]]}`)
  76. }, 300)
  77. },
  78. }
  79. })