dateTimePicker.js 2.5 KB

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