datetimepicker.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // components/datatimepicker/datatimepicker.js
  2. Component({
  3. behaviors: ['wx://form-field'],
  4. externalClasses: ["i-class"],
  5. properties: {
  6. format: {
  7. type: String
  8. },
  9. name: {
  10. type: String
  11. },
  12. placeholder: {
  13. type: String
  14. }
  15. },
  16. data: {},
  17. lifetimes: {
  18. attached: function() {
  19. //当前时间 年月日 时分秒
  20. const date = new Date()
  21. const curYear = date.getFullYear()
  22. const curMonth = date.getMonth() + 1
  23. const curDay = date.getDate()
  24. const curHour = date.getHours()
  25. const curMinute = date.getMinutes()
  26. const curSecond = date.getSeconds()
  27. //初始化时间选择轴
  28. this.setData({
  29. chooseYear: curYear
  30. })
  31. this.initColumn(curMonth);
  32. if(curMonth==2) this.setData({ days: this.data.multiArray[2] })
  33. //不足两位的前面好补0 因为后面要获取在时间轴上的索引 时间轴初始化的时候都是两位
  34. var showMonth = curMonth < 10 ? ('0' + curMonth) : curMonth
  35. var showDay = curDay < 10 ? ('0' + curDay) : curDay
  36. var showHour = curHour < 10 ? ('0' + curHour) : curHour
  37. var showMinute = curMinute < 10 ? ('0' + curMinute) : curMinute
  38. var showSecond = curSecond < 10 ? ('0' + curSecond) : curSecond
  39. //当前时间在picker列上面的索引 为了当打开时间选择轴时选中当前的时间
  40. var indexYear = this.data.years.indexOf(curYear + '')
  41. var indexMonth = this.data.months.indexOf(showMonth + '')
  42. var indexDay = this.data.days.indexOf(showDay + '')
  43. var indexHour = this.data.hours.indexOf(showHour + '')
  44. var indexMinute = this.data.minutes.indexOf(showMinute + '')
  45. var indexSecond = this.data.seconds.indexOf(showSecond + '')
  46. var multiIndex = []
  47. var multiArray = []
  48. var value = ''
  49. var format = this.properties.format;
  50. if (format == 'yyyy-MM-dd') {
  51. multiIndex = [indexYear, indexMonth, indexDay]
  52. value = `${curYear}-${showMonth}-${showDay}`
  53. multiArray = [this.data.years, this.data.months, this.data.days]
  54. }
  55. if (format == 'HH:mm:ss') {
  56. multiIndex = [indexHour, indexMinute, indexSecond]
  57. value = `${showHour}:${showMinute}:${showSecond}`
  58. multiArray = [this.data.hours, this.data.minutes, this.data.seconds]
  59. }
  60. if (format == 'yyyy-MM-dd HH:mm') {
  61. multiIndex = [indexYear, indexMonth, indexDay, indexHour, indexMinute]
  62. value = `${curYear}-${showMonth}-${showDay} ${showHour}:${showMinute}`
  63. multiArray = [this.data.years, this.data.months, this.data.days, this.data.hours, this.data.minutes]
  64. }
  65. if (format == 'yyyy-MM-dd HH:mm:ss') {
  66. multiIndex = [indexYear, indexMonth, indexDay, indexHour, indexMinute, indexSecond]
  67. value = `${curYear}-${showMonth}-${showDay} ${showHour}:${showMinute}:${showSecond}`
  68. multiArray = [this.data.years, this.data.months, this.data.days, this.data.hours, this.data.minutes, this.data.seconds]
  69. }
  70. this.setData({
  71. // value, // 初始时间
  72. multiIndex,
  73. multiArray,
  74. curMonth,
  75. chooseYear: curYear,
  76. })
  77. }
  78. },
  79. /**
  80. * 组件的方法列表
  81. */
  82. methods: {
  83. //获取时间日期
  84. bindPickerChange: function(e) {
  85. this.setData({
  86. multiIndex: e.detail.value
  87. })
  88. const index = this.data.multiIndex
  89. var format = this.properties.format
  90. var showTime = ''
  91. if (format == 'yyyy-MM-dd') {
  92. const year = this.data.multiArray[0][index[0]]
  93. const month = this.data.multiArray[1][index[1]]
  94. const day = this.data.multiArray[2][index[2]]
  95. showTime = `${year}-${month}-${day}`
  96. }
  97. if (format == 'HH:mm:ss') {
  98. const hour = this.data.multiArray[0][index[0]]
  99. const minute = this.data.multiArray[1][index[1]]
  100. const second = this.data.multiArray[2][index[2]]
  101. showTime = `${hour}:${minute}:${second}`
  102. }
  103. if (format == 'yyyy-MM-dd HH:mm') {
  104. const year = this.data.multiArray[0][index[0]]
  105. const month = this.data.multiArray[1][index[1]]
  106. const day = this.data.multiArray[2][index[2]]
  107. const hour = this.data.multiArray[3][index[3]]
  108. const minute = this.data.multiArray[4][index[4]]
  109. showTime = `${year}-${month}-${day} ${hour}:${minute}`
  110. }
  111. if (format == 'yyyy-MM-dd HH:mm:ss') {
  112. const year = this.data.multiArray[0][index[0]]
  113. const month = this.data.multiArray[1][index[1]]
  114. const day = this.data.multiArray[2][index[2]]
  115. const hour = this.data.multiArray[3][index[3]]
  116. const minute = this.data.multiArray[4][index[4]]
  117. const second = this.data.multiArray[5][index[5]]
  118. showTime = `${year}-${month}-${day} ${hour}:${minute}:${second}`
  119. }
  120. this.setData({
  121. value: showTime
  122. })
  123. this.triggerEvent('dateTimePicker', showTime)
  124. },
  125. //初始化时间选择轴
  126. initColumn(curMonth) {
  127. const years = []
  128. const months = []
  129. const days = []
  130. const hours = []
  131. const minutes = []
  132. const seconds = []
  133. for (let i = 2019; i <= 2099; i++) {
  134. years.push(i + '')
  135. }
  136. for (let i = 1; i <= 12; i++) {
  137. if (i < 10) {
  138. i = "0" + i;
  139. }
  140. months.push(i + '')
  141. }
  142. if (curMonth == 1 || curMonth == 3 || curMonth == 5 || curMonth == 7 || curMonth == 8 || curMonth == 10 || curMonth == 12) {
  143. for (let i = 1; i <= 31; i++) {
  144. if (i < 10) {
  145. i = "0" + i;
  146. }
  147. days.push(i + '')
  148. }
  149. }
  150. if (curMonth == 4 || curMonth == 6 || curMonth == 9 || curMonth == 11) {
  151. for (let i = 1; i <= 30; i++) {
  152. if (i < 10) {
  153. i = "0" + i;
  154. }
  155. days.push(i + '')
  156. }
  157. }
  158. if (curMonth == 2) {
  159. this.setFebDays()
  160. }
  161. for (let i = 0; i <= 23; i++) {
  162. if (i < 10) {
  163. i = "0" + i;
  164. }
  165. hours.push(i + '')
  166. }
  167. for (let i = 0; i <= 59; i++) {
  168. if (i < 10) {
  169. i = "0" + i;
  170. }
  171. minutes.push(i + '')
  172. }
  173. for (let i = 0; i <= 59; i++) {
  174. if (i < 10) {
  175. i = "0" + i;
  176. }
  177. seconds.push(i + '')
  178. }
  179. this.setData({
  180. years,
  181. months,
  182. days,
  183. hours,
  184. minutes,
  185. seconds
  186. })
  187. },
  188. /**
  189. * 列改变时触发
  190. */
  191. bindPickerColumnChange: function(e) {
  192. //获取年份 用于计算改年的2月份为平年还是闰年
  193. if (e.detail.column == 0 && this.properties.format != 'HH:mm:ss') {
  194. var chooseYear = this.data.multiArray[e.detail.column][e.detail.value];
  195. this.setData({
  196. chooseYear
  197. })
  198. if (this.data.curMonth == '02' || this.data.chooseMonth == '02') {
  199. this.setFebDays()
  200. }
  201. }
  202. //当前第二为月份时需要初始化当月的天数
  203. if (e.detail.column == 1 && this.properties.format != 'HH:mm:ss') {
  204. let num = parseInt(this.data.multiArray[e.detail.column][e.detail.value]);
  205. let temp = [];
  206. if (num == 1 || num == 3 || num == 5 || num == 7 || num == 8 || num == 10 || num == 12) { //31天的月份
  207. for (let i = 1; i <= 31; i++) {
  208. if (i < 10) {
  209. i = "0" + i;
  210. }
  211. temp.push("" + i);
  212. }
  213. this.setData({
  214. ['multiArray[2]']: temp
  215. });
  216. } else if (num == 4 || num == 6 || num == 9 || num == 11) { //30天的月份
  217. for (let i = 1; i <= 30; i++) {
  218. if (i < 10) {
  219. i = "0" + i;
  220. }
  221. temp.push("" + i);
  222. }
  223. this.setData({
  224. ['multiArray[2]']: temp
  225. });
  226. } else if (num == 2) { //2月份天数
  227. this.setFebDays()
  228. }
  229. }
  230. var data = {
  231. multiArray: this.data.multiArray,
  232. multiIndex: this.data.multiIndex
  233. };
  234. data.multiIndex[e.detail.column] = e.detail.value;
  235. this.setData(data);
  236. },
  237. //计算二月份天数
  238. setFebDays() {
  239. let year = parseInt(this.data.chooseYear);
  240. let temp = [];
  241. if (year % (year % 100 ? 4 : 400) ? false : true) {
  242. for (let i = 1; i <= 29; i++) {
  243. if (i < 10) {
  244. i = "0" + i;
  245. }
  246. temp.push("" + i);
  247. }
  248. this.setData({
  249. ['multiArray[2]']: temp,
  250. chooseMonth: '02'
  251. });
  252. } else {
  253. for (let i = 1; i <= 28; i++) {
  254. if (i < 10) {
  255. i = "0" + i;
  256. }
  257. temp.push("" + i);
  258. }
  259. this.setData({
  260. ['multiArray[2]']: temp,
  261. chooseMonth: '02'
  262. });
  263. }
  264. }
  265. },
  266. })