dateTimePicker.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. function withData(param) {
  2. return param < 10 ? '0' + param : '' + param;
  3. }
  4. function getLoopArray(start, end) {
  5. var start = start || 0;
  6. var end = end || 1;
  7. var array = [];
  8. for (var i = start; i <= end; i++) {
  9. array.push(withData(i));
  10. }
  11. return array;
  12. }
  13. function getMonthDay(year, month) {
  14. var flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0), array = null;
  15. switch (month) {
  16. case '01':
  17. case '03':
  18. case '05':
  19. case '07':
  20. case '08':
  21. case '10':
  22. case '12':
  23. array = getLoopArray(1, 31)
  24. break;
  25. case '04':
  26. case '06':
  27. case '09':
  28. case '11':
  29. array = getLoopArray(1, 30)
  30. break;
  31. case '02':
  32. array = flag ? getLoopArray(1, 29) : getLoopArray(1, 28)
  33. break;
  34. default:
  35. array = '月份格式不正确,请重新输入!'
  36. }
  37. return array;
  38. }
  39. function getNewDateArry() {
  40. // 当前时间的处理
  41. var newDate = new Date();
  42. var year = withData(newDate.getFullYear()),
  43. mont = withData(newDate.getMonth() + 1),
  44. date = withData(newDate.getDate()),
  45. hour = withData(newDate.getHours()),
  46. minu = withData(newDate.getMinutes()),
  47. seco = withData(newDate.getSeconds());
  48. return [year, mont, date, hour, minu, seco];
  49. }
  50. function dateTimePicker(startYear, endYear, date) {
  51. // 返回默认显示的数组和联动数组的声明
  52. var dateTime = [], dateTimeArray = [[], [], [], [], [], []];
  53. var start = startYear || 1978;
  54. var end = endYear || 2100;
  55. // 默认开始显示数据
  56. var defaultDate = date ? [...date.split(' ')[0].split('-'), ...date.split(' ')[1].split(':')] : getNewDateArry();
  57. // 处理联动列表数据
  58. /*年月日 时分秒*/
  59. dateTimeArray[0] = getLoopArray(start, end);
  60. dateTimeArray[1] = getLoopArray(1, 12);
  61. dateTimeArray[2] = getMonthDay(defaultDate[0], defaultDate[1]);
  62. dateTimeArray[3] = getLoopArray(0, 23);
  63. dateTimeArray[4] = getLoopArray(0, 59);
  64. dateTimeArray[5] = getLoopArray(0, 59);
  65. dateTimeArray.forEach((current, index) => {
  66. dateTime.push(current.indexOf(defaultDate[index]));
  67. });
  68. return {
  69. dateTimeArray: dateTimeArray,
  70. dateTime: dateTime
  71. }
  72. }
  73. module.exports = {
  74. dateTimePicker: dateTimePicker,
  75. getMonthDay: getMonthDay
  76. }