datePicker.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. class BaseInfo {
  2. constructor() { this.newDate = new Date(); }
  3. withData(param) {
  4. return parseInt(param) < 10 ? '0' + param : '' + param;
  5. }
  6. getLoopArray(start, end) {
  7. var start = start || 0;
  8. var end = end || 0;
  9. var array = [];
  10. for (var i = start; i <= end; i++) {
  11. array.push(this.withData(i));
  12. }
  13. return array;
  14. }
  15. formatArr(dateString) {
  16. return [...dateString.split(' ')[0].split('-'), ...dateString.split(' ')[1].split(':')]
  17. }
  18. beforeDateArr(disYear) {
  19. /*
  20. * 处理前
  21. * 获取当前时间
  22. */
  23. let year = this.newDate.getFullYear() - (disYear || 0)
  24. let month = this.newDate.getMonth() + 1
  25. let day = this.newDate.getDate()
  26. let hour = this.newDate.getHours()
  27. let minute = this.newDate.getMinutes()
  28. return [year, month, day, hour, minute]
  29. }
  30. afterDateArr() {
  31. /*
  32. * 处理后
  33. * 获取当前时间
  34. */
  35. let year = this.withData(this.newDate.getFullYear())
  36. let mont = this.withData(this.newDate.getMonth() + 1)
  37. let date = this.withData(this.newDate.getDate())
  38. let hour = this.withData(this.newDate.getHours())
  39. let minu = this.withData(this.newDate.getMinutes())
  40. return [year, mont, date, hour, minu];
  41. }
  42. }
  43. // 实现
  44. class dateTimePicker extends BaseInfo {
  45. constructor(startDate, endDate, defaultDate) {
  46. super();
  47. this.dateTimeArray = null
  48. this.dateTime = null
  49. this.startDate = super.formatArr(startDate); // 开始时间
  50. this.endDate = endDate ? super.formatArr(endDate) : super.afterDateArr(); // 结束时间
  51. this.defaultDate = defaultDate ? super.formatArr(defaultDate) : this.startDate;
  52. }
  53. setValue(obj) {
  54. for (let key in obj) {
  55. this[key] = obj[key]
  56. }
  57. }
  58. /* 获取当前切换选择的日期值*/
  59. getCurDateInfo() {
  60. return this.dateTime && this.dateTimeArray ? {
  61. year: this.dateTimeArray[0][this.dateTime[0]],
  62. month: this.dateTimeArray[1][this.dateTime[1]],
  63. day: this.dateTimeArray[2][this.dateTime[2]],
  64. hour: this.dateTimeArray[3][this.dateTime[3]],
  65. second: this.dateTimeArray[4][this.dateTime[4]],
  66. } : {}
  67. }
  68. /* 获取月数组*/
  69. getMonths() {
  70. let array = []
  71. const year = (this.getCurDateInfo().year || this.defaultDate[0]).replace(/年/, '');
  72. if (this.startDate[0] == this.endDate[0]) {
  73. /*
  74. * 开始的年和结束的年相同
  75. * 就取(开始月,结束月)
  76. */
  77. array = super.getLoopArray(parseInt(this.startDate[1]), parseInt(this.endDate[1]))
  78. } else {
  79. switch (year) {
  80. case this.startDate[0]:
  81. /* 开始年 */
  82. array = super.getLoopArray(parseInt(this.startDate[1]), 12)
  83. break;
  84. case this.endDate[0]:
  85. /* 结束年 */
  86. array = super.getLoopArray(1, parseInt(this.endDate[1]))
  87. break;
  88. default:
  89. array = super.getLoopArray(1, 12)
  90. break;
  91. }
  92. }
  93. return array;
  94. }
  95. /* 获取日数组*/
  96. getDays() {
  97. let array = []
  98. let lastDay = null
  99. const year = (this.getCurDateInfo().year || this.defaultDate[0]).replace(/年/, '');
  100. const month = (this.getCurDateInfo().month || this.defaultDate[1]).replace(/月/, '');
  101. const flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
  102. switch (month) {
  103. case '01':
  104. case '03':
  105. case '05':
  106. case '07':
  107. case '08':
  108. case '10':
  109. case '12':
  110. lastDay = 31
  111. break;
  112. case '04':
  113. case '06':
  114. case '09':
  115. case '11':
  116. lastDay = 30
  117. break;
  118. case '02':
  119. lastDay = flag ? 29 : 28
  120. break;
  121. default:
  122. array = '月份格式不正确,请重新输入!'
  123. }
  124. const afterDateArr = super.afterDateArr()
  125. const _start = year == this.startDate[0] && month == this.startDate[1]
  126. const _end = year == this.endDate[0] && month == this.endDate[1]
  127. if (this.startDate[0] == this.endDate[0] && this.startDate[1] == this.endDate[1]) {
  128. /*
  129. * 开始的年和结束的年相同,开始月和结束月相同
  130. * 就取(开始日,结束日)
  131. */
  132. array = super.getLoopArray(parseInt(this.startDate[2]), parseInt(this.endDate[2]))
  133. } else {
  134. if (_start) { // 开始年月
  135. array = super.getLoopArray(parseInt(this.startDate[2]), lastDay)
  136. } else if (_end) { // 结束年月
  137. array = super.getLoopArray(1, parseInt(this.endDate[2]))
  138. } else {
  139. array = super.getLoopArray(1, lastDay)
  140. }
  141. }
  142. return array;
  143. }
  144. /* 获取小时数组*/
  145. getHours() {
  146. let array = []
  147. const year = (this.getCurDateInfo().year || this.defaultDate[0]).replace(/年/, '');
  148. const month = (this.getCurDateInfo().month || this.defaultDate[1]).replace(/月/, '');
  149. const day = (this.getCurDateInfo().day || this.defaultDate[2]).replace(/日/, '');
  150. const _start = year == this.startDate[0] && month == this.startDate[1] && day == this.startDate[2]
  151. const _end = year == this.endDate[0] && month == this.endDate[1] && day == this.endDate[2]
  152. const _equal = this.startDate[0] == this.endDate[0] && this.startDate[1] == this.endDate[1] && this.startDate[
  153. 2] == this.endDate[2]
  154. if (_equal) {
  155. /*
  156. * 开始的年月日和结束的年月日都相同
  157. * 就取(开始小时,结束小时)
  158. */
  159. array = super.getLoopArray(parseInt(this.startDate[3]), parseInt(this.endDate[3]))
  160. } else {
  161. if (_start) { // 开始年月日
  162. array = super.getLoopArray(parseInt(this.startDate[3]), 23)
  163. } else if (_end) { // 结尾年月日
  164. array = super.getLoopArray(0, parseInt(this.endDate[3]))
  165. } else {
  166. array = super.getLoopArray(0, 23)
  167. }
  168. }
  169. return array;
  170. }
  171. /* 获取分钟数组*/
  172. getMinutes(years, months, days, hours) {
  173. let array = []
  174. const year = (this.getCurDateInfo().year || this.defaultDate[0]).replace(/年/, '');
  175. const month = (this.getCurDateInfo().month || this.defaultDate[1]).replace(/月/, '');
  176. const day = (this.getCurDateInfo().day || this.defaultDate[2]).replace(/日/, '');
  177. const hour = (this.getCurDateInfo().hour || this.defaultDate[3]).replace(/时/, '');
  178. const _start = year == this.startDate[0] && month == this.startDate[1] && day == this.startDate[2] && hour == this
  179. .startDate[3]
  180. const _end = year == this.endDate[0] && month == this.endDate[1] && day == this.endDate[2] && hour == this
  181. .endDate[3]
  182. const _equal = this.startDate[0] == this.endDate[0] && this.startDate[1] == this.endDate[1] && this.startDate[
  183. 2] == this.endDate[2] && this.startDate[3] == this.endDate[3]
  184. if (_equal) {
  185. /*
  186. * 开始的年月日时和结束的年月日时都相同
  187. * 就取(开始小时,结束小时)
  188. */
  189. array = super.getLoopArray(parseInt(this.startDate[4]), parseInt(this.endDate[4]))
  190. } else {
  191. if (_start) { // 开始年月日
  192. array = super.getLoopArray(parseInt(this.startDate[4]), 59)
  193. } else if (_end) { // 结尾年月日
  194. array = super.getLoopArray(0, parseInt(this.endDate[4]))
  195. } else {
  196. array = super.getLoopArray(0, 59)
  197. }
  198. }
  199. return array;
  200. }
  201. /* */
  202. dispatch(index) {
  203. let arr = []
  204. switch (index) {
  205. case 0:
  206. arr = super.getLoopArray(this.startDate[0], this.endDate[0]);
  207. break;
  208. case 1:
  209. arr = this.getMonths();
  210. break;
  211. case 2:
  212. arr = this.getDays();
  213. break;
  214. case 3:
  215. arr = this.getHours();
  216. break;
  217. case 4:
  218. arr = this.getMinutes();
  219. break;
  220. default:
  221. break;
  222. }
  223. return arr
  224. }
  225. /* 初始默认数据 */
  226. render() {
  227. const dateTime = []
  228. const dateTimeArray = [
  229. [],
  230. [],
  231. [],
  232. [],
  233. []
  234. ];
  235. /*年月日 时分秒*/
  236. for (let i = 0; i < dateTimeArray.length; i++) {
  237. dateTimeArray[i] = this.dispatch(i)
  238. }
  239. dateTimeArray.forEach((current, index) => {
  240. const _index = current.indexOf(this.defaultDate[index])
  241. dateTime.push(_index == -1 ? 0 : _index);
  242. });
  243. return {
  244. dateTimeArray,
  245. dateTime
  246. }
  247. }
  248. }
  249. function newDateTimePicker(startDateTime, endDateTime, pText) {
  250. let newDateTimePicker = new dateTimePicker(startDateTime, endDateTime, pText)
  251. return newDateTimePicker
  252. }
  253. module.exports = {
  254. newDateTimePicker: newDateTimePicker,
  255. }