util.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const formatTime = date => {
  2. console.log(date);
  3. const day = parseInt(date / 1000 / 60 / 60 / 24);
  4. const hour = parseInt(date / 1000 / 60 / 60 % 24);
  5. const minute = parseInt(date / 1000 / 60 % 60);
  6. const arr = [hour, minute].map(formatNumber);
  7. arr.unshift(day);
  8. return arr
  9. }
  10. const lastHours = date => {
  11. let h = parseInt((date % (1000 * 60 * 60 * 72)) / (1000 * 60 * 60));
  12. let m = parseInt((date % (1000 * 60 * 60)) / (1000 * 60));
  13. return h + '小时' + m + '分'
  14. }
  15. const addThreeYears = date => {
  16. // const t = date + (31536000000 * 3)
  17. const t = date
  18. return formatDate(t, 4)
  19. }
  20. const lastHoursInGroup = date => {
  21. // const temp = [];
  22. let h = parseInt((date % (1000 * 60 * 60 * 72)) / (1000 * 60 * 60));
  23. let m = parseInt((date % (1000 * 60 * 60)) / (1000 * 60));
  24. return [h.toString(), m.toString()]
  25. }
  26. const formatNumber = n => {
  27. n = n.toString()
  28. return n[1] ? n : '0' + n
  29. }
  30. //时间戳转时间
  31. function formatDate(time, flag) {
  32. const t = new Date(time);
  33. const tf = function (i) {
  34. return (i < 10 ? '0' : '') + i
  35. };
  36. const year = t.getFullYear();
  37. const month = tf(t.getMonth() + 1);
  38. const day = tf(t.getDate());
  39. const hour = tf(t.getHours());
  40. const minute = tf(t.getMinutes());
  41. //console.log( month + '月' + day + '日' + hour + ':' + minute);
  42. if (flag == 1) {
  43. return month + '月' + day + '日' + ' ' + hour + ':' + minute;
  44. } else if (flag == 2) {
  45. console.log(year, month, day)
  46. return year + '-' + month + '-' + day
  47. } else if (flag == 3) {
  48. return month + '-' + day + ' ' + hour + ':' + minute;
  49. } else if (flag == 4) {
  50. return year + '年' + month + '月' + day + '日';
  51. } else if (flag == 5) {
  52. return day.toString().substring(1, 2)
  53. } else if (flag == 6) {
  54. return month + '.' + day;
  55. }
  56. }
  57. function GetQueryString(str, name) {
  58. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  59. var r = str.match(reg);
  60. if (r != null) return unescape(r[2]);
  61. return null;
  62. }
  63. function gradeFilter(grade) {
  64. console.log(grade);
  65. let gradeObj = {
  66. "PRIMARY_FIRST_GRADE": "一年级",
  67. "PRIMARY_SECOND_GRADE": "二年级",
  68. "PRIMARY_THREE_GRADE": "三年级",
  69. "PRIMARY_SENIOR_GRADE": "四年级",
  70. "PRESCHOOL": "学前班",
  71. }
  72. return gradeObj[grade]
  73. }
  74. module.exports = {
  75. formatTime: formatTime,
  76. formatDate,
  77. GetQueryString,
  78. lastHours,
  79. lastHoursInGroup,
  80. addThreeYears,
  81. gradeFilter
  82. }