date-time-picker.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // components/date-time-picker/date-time-picker.js
  2. const leftPad0 = function(v, n) {
  3. if (!v) {
  4. v = "";
  5. }
  6. let prefix = "";
  7. for (let i = 0; i < n; i++) {
  8. prefix += "0";
  9. }
  10. return (prefix + v).substr(-n);
  11. };
  12. const stringToDate = function(str) {
  13. str = str.replace(/-/g, "/");
  14. return new Date(str);
  15. };
  16. const isLeapYear = function(year) {
  17. if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)) {
  18. return true;
  19. }
  20. return false;
  21. };
  22. const now = new Date();
  23. const years = [];
  24. const beginYear = 2020;
  25. for (var i = beginYear; i <= (now.getFullYear() + 10); i++) {
  26. years.push(i + "年");
  27. }
  28. const months = [];
  29. for (var i = 0; i < 12; i++) {
  30. months.push(leftPad0(i + 1, 2) + "月");
  31. }
  32. const days = [];
  33. for (var i = 0; i < 31; i++) {
  34. days.push(leftPad0(i + 1, 2) + "日");
  35. }
  36. const hours = [];
  37. for (var i = 0; i < 24; i++) {
  38. hours.push(leftPad0(i, 2) + "时");
  39. }
  40. const minutes = [];
  41. for (var i = 0; i < 60; i++) {
  42. minutes.push(leftPad0(i, 2) + "分");
  43. }
  44. // const seconds = [];
  45. // for (var i = 0; i < 60; i++) {
  46. // seconds.push(leftPad0(i, 2) + "秒");
  47. // }
  48. Component({
  49. /**
  50. * 组件的属性列表
  51. */
  52. properties: {
  53. value: String,
  54. dateValue: {
  55. type: Date
  56. }
  57. },
  58. /**
  59. * 组件的初始数据
  60. */
  61. data: {
  62. // valueArray: [0, 0, 0, 0, 0, 0],
  63. valueArray: [0, 0, 0, 0, 0],
  64. rangeValues: [
  65. years,
  66. months,
  67. days,
  68. hours,
  69. minutes,
  70. // seconds
  71. ],
  72. pickerYear: beginYear,
  73. pickerMonth: 1
  74. },
  75. observers: {
  76. value: function(v) {
  77. this.setData({
  78. valueArray: this._dateToValueArray(stringToDate(v))
  79. })
  80. },
  81. dateValue: function(date) {
  82. this.setData({
  83. valueArray: this._dateToValueArray(date)
  84. })
  85. },
  86. valueArray: function(v) {
  87. this._settMonthDays(v[0] + beginYear, v[1] + 1);
  88. }
  89. },
  90. /**
  91. * 组件的方法列表
  92. */
  93. methods: {
  94. _dateToValueArray(date) {
  95. // return [date.getFullYear() - beginYear, date.getMonth(), date.getDate() - 1, date.getHours(), date.getMinutes(), date.getSeconds()];
  96. return [date.getFullYear() - beginYear, date.getMonth(), date.getDate() - 1, date.getHours(), date.getMinutes()];
  97. },
  98. _settMonthDays(year, month) {
  99. let monthDays = 31;
  100. switch (month) {
  101. case 2:
  102. monthDays = 28;
  103. if (isLeapYear(year)) {
  104. monthDays = 29;
  105. }
  106. break;
  107. case 4:
  108. case 6:
  109. case 9:
  110. case 11:
  111. monthDays = 30;
  112. break;
  113. }
  114. let days = [];
  115. for (let i = 0; i < monthDays; i++) {
  116. days.push(leftPad0(i + 1, 2) + "日");
  117. }
  118. this.setData({
  119. pickerYear: year,
  120. pickerMonth: month,
  121. "rangeValues[2]": days
  122. });
  123. },
  124. handleCancel(e) {
  125. this.setData({
  126. valueArray: this.data.valueArray
  127. })
  128. },
  129. handleColumnChange(e) {
  130. if (e.detail.column > 1) return false;
  131. let year = this.data.pickerYear;
  132. let month = this.data.pickerMonth;
  133. if (e.detail.column == 0) {
  134. year = e.detail.value + beginYear;
  135. } else if (e.detail.column == 1) {
  136. month = e.detail.value + 1;
  137. }
  138. this._settMonthDays(year, month);
  139. },
  140. handleValueChange(e) {
  141. let dateArr = [];
  142. for (let i in e.detail.value) {
  143. let v = this.data.rangeValues[i][e.detail.value[i]];
  144. dateArr.push(v.toString().substr(0, v.length - 1))
  145. }
  146. // let dateString = dateArr[0] + "-" + dateArr[1] + "-" + dateArr[2] + " " + dateArr[3] + ":" + dateArr[4] + ":" + dateArr[5];
  147. let dateString = dateArr[0] + "-" + dateArr[1] + "-" + dateArr[2] + " " + dateArr[3] + ":" + dateArr[4];
  148. this.triggerEvent('change', {
  149. date: stringToDate(dateString),
  150. dateString
  151. })
  152. }
  153. }
  154. })