util.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const formatTime = date => {
  2. const year = date.getFullYear();
  3. const month = date.getMonth() + 1;
  4. const day = date.getDate();
  5. const hour = date.getHours();
  6. const minute = date.getMinutes();
  7. const second = date.getSeconds();
  8. return `${[year, month, day].map(formatNumber).join("/")} ${[
  9. hour,
  10. minute,
  11. second
  12. ]
  13. .map(formatNumber)
  14. .join(":")}`;
  15. };
  16. const formatNumber = n => {
  17. n = n.toString();
  18. return n[1] ? n : `0${n}`;
  19. }; //获取url参数组成json
  20. const GetQueryJson = e => {
  21. if (e == "undefined") {
  22. return {};
  23. } else {
  24. let url = e; //获取url
  25. let arr = []; //存储参数的数
  26. let res = {}; //存储最终JSON结果对象
  27. arr = url.split("?")[1].split("&"); //获取浏览器地址栏中的参数
  28. for (let i = 0; i < arr.length; i++) {
  29. //遍历参数
  30. if (arr[i].indexOf("=") != -1) {
  31. //如果参数中有值
  32. let str = arr[i].split("=");
  33. res[str[0]] = str[1];
  34. } else {
  35. //如果参数中无值
  36. res[arr[i]] = "";
  37. }
  38. }
  39. return res;
  40. }
  41. };
  42. module.exports = {
  43. formatTime,
  44. GetQueryJson
  45. };