datePicker.js 8.3 KB

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