dateTimePicker.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //设置日期的两位数显示
  2. function withData(param) {
  3. return param < 10 ? '0' + param : '' + param;
  4. }
  5. function getLoopArray(start, end) {
  6. var start = start || 0;
  7. var end = end || 1;
  8. var array = [];
  9. for (var i = start; i <= end; i++) {
  10. array.push(withData(i));
  11. }
  12. return array;
  13. }
  14. function getMonthDay(year, month) {
  15. var flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0), array = null;
  16. switch (month) {
  17. case '01':
  18. case '03':
  19. case '05':
  20. case '07':
  21. case '08':
  22. case '10':
  23. case '12':
  24. array = getLoopArray(1, 31)
  25. break;
  26. case '04':
  27. case '06':
  28. case '09':
  29. case '11':
  30. array = getLoopArray(1, 30)
  31. break;
  32. case '02':
  33. array = flag ? getLoopArray(1, 29) : getLoopArray(1, 28)
  34. break;
  35. default:
  36. array = '月份格式不正确,请重新输入!'
  37. }
  38. return array;
  39. }
  40. function getNewDateArry() {
  41. // 当前时间的处理
  42. var newDate = new Date();
  43. var year = withData(newDate.getFullYear()),
  44. mont = withData(newDate.getMonth() + 1),
  45. date = withData(newDate.getDate()),
  46. hour = withData(newDate.getHours()),
  47. minu = withData(newDate.getMinutes()),
  48. seco = withData(newDate.getSeconds());
  49. return [year, mont, date, hour, minu, seco];
  50. }
  51. /**
  52. * startYear: 开始的年份,进行年份的范围指定
  53. * endYear: 结束的年份,进行年份的范围指定
  54. * date: 年份日期
  55. * 1、如果不传参数,默认显示当前日期和时间
  56. * 2、如果只需要date参数,将startYear和endYear设置为空字符串
  57. */
  58. function dateTimePicker(startYear, endYear, date) {
  59. // 返回默认显示的数组和联动数组的声明
  60. var dateTime = [], dateTimeArray = [[], [], [], [], []];
  61. var start = startYear || 1978;
  62. var end = endYear || 2100;
  63. // 默认开始显示数据
  64. var defaultDate = date ? [...date.split(' ')[0].split('-'), ...date.split(' ')[1].split(':')] : getNewDateArry();
  65. // 处理联动列表数据
  66. /*年月日 时分秒*/
  67. dateTimeArray[0] = getLoopArray(start, end);
  68. dateTimeArray[1] = getLoopArray(1, 12);
  69. dateTimeArray[2] = getMonthDay(defaultDate[0], defaultDate[1]);
  70. dateTimeArray[3] = getLoopArray(0, 23);
  71. // dateTimeArray[4] = getLoopArray(0, 59);
  72. dateTimeArray[4] = ['00','30'];
  73. // dateTimeArray[5] = getLoopArray(0, 59);
  74. dateTimeArray.forEach((current, index) => {
  75. dateTime.push(current.indexOf(defaultDate[index]));
  76. });
  77. return {
  78. dateTimeArray: dateTimeArray,
  79. dateTime: dateTime
  80. }
  81. }
  82. /** 对数组中的时间信息进行格式化
  83. * dateTimeArray :通过dateTimePicker获取的日期范围数组
  84. * dateTime: 选择的日期数组信息
  85. */
  86. function formatPickerDateTime(dateTimeArray,dateTime) {
  87. var obj = dateTimePicker()
  88. var format = dateTimeArray[0][dateTime[0]] + '-' + dateTimeArray[1][dateTime[1]]
  89. + '-' + dateTimeArray[2][dateTime[2]] + " " +
  90. dateTimeArray[3][dateTime[3]] + ':' + dateTimeArray[4][dateTime[4]]
  91. // + ':' + dateTimeArray[5][dateTime[5]]
  92. return format
  93. }
  94. module.exports = {
  95. dateTimePicker: dateTimePicker,
  96. getMonthDay: getMonthDay,
  97. formatPickerDateTime: formatPickerDateTime
  98. }