index.js 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // commpents/datetime-picker/index.js
  2. const app = getApp()
  3. const moment = require("../../utils/moment.min");
  4. Component({
  5. /**
  6. * 组件的属性列表
  7. */
  8. options: { multipleSlots: true },
  9. properties: {
  10. datetime: { type: String, value: '' },
  11. name: { type: String, value: '' }
  12. },
  13. // 生命周期函数,可以为函数,或一个在methods段中定义的方法名
  14. attached: function () { }, // 此处attached的声明会被lifetimes字段中的声明覆盖
  15. ready: function () { },
  16. pageLifetimes: {
  17. // 组件所在页面的生命周期函数
  18. show: function () {
  19. const that = this;
  20. that.search();
  21. },
  22. hide: function () { },
  23. resize: function () { },
  24. },
  25. /**
  26. * 组件的初始数据
  27. */
  28. data: {
  29. dateTimeArray: [],
  30. // 计算日期
  31. year: '',
  32. moneh: '',
  33. },
  34. /**
  35. * 组件的方法列表
  36. */
  37. methods: {
  38. // 查询时间
  39. search: function () {
  40. const that = this;
  41. // 计算年份
  42. let year = [];
  43. let startYear = moment().format('YYYY');
  44. let endYear = Number(startYear) + 100;
  45. for (var i = startYear; i <= endYear; i++) { year.push(String(i)) }
  46. // 数组数据, 年, 月, 日, 时, 分, 秒;
  47. let dateTimeArray = [
  48. [...year],
  49. ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'],
  50. ['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'],
  51. ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23'],
  52. ['00', '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', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59'],
  53. ['00', '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', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59'],
  54. ]
  55. that.setData({ dateTimeArray })
  56. },
  57. // 确认选择
  58. change: function (e) {
  59. const that = this;
  60. let dateTimeArray = that.data.dateTimeArray;
  61. let value = e.detail.value;
  62. let datetime = dateTimeArray[0][value[0]] + '-' + dateTimeArray[1][value[1]] + '-' + dateTimeArray[2][value[2]] + ' ' + dateTimeArray[3][value[3]] + ':' + dateTimeArray[4][value[4]] + ':' + dateTimeArray[5][value[5]];
  63. that.triggerEvent('datetimeChange', { datetime: datetime, name: that.properties.name })
  64. },
  65. // 选择月份
  66. columnChange: function (e) {
  67. const that = this;
  68. const { column, value } = e.detail;
  69. let dateTimeArray = that.data.dateTimeArray;
  70. let year = '';
  71. let month = '';
  72. year = that.data.year || dateTimeArray[0][0];
  73. month = that.data.month || dateTimeArray[1][0];
  74. if (column == '0') year = dateTimeArray[0][value];
  75. if (column == '1') month = dateTimeArray[1][value];
  76. that.setData({ year: year, month: month });
  77. // 计算当前选择年月所要显示的日期
  78. let ym = that.data.year + '-' + that.data.month;
  79. let test = ['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'];
  80. let monthDay = moment(ym).daysInMonth();
  81. let thr = test.splice(0, monthDay);
  82. that.setData({ 'dateTimeArray[2]': thr })
  83. },
  84. }
  85. })