util.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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){return (i < 10 ? '0' : '') + i};
  34. const year = t.getFullYear();
  35. const month = tf(t.getMonth() + 1);
  36. const day = tf(t.getDate());
  37. const hour = tf(t.getHours());
  38. const minute = tf(t.getMinutes());
  39. //console.log( month + '月' + day + '日' + hour + ':' + minute);
  40. if(flag == 1) {
  41. return month + '月' + day + '日' + ' ' + hour + ':' + minute;
  42. }else if(flag == 2) {
  43. console.log(year,month,day)
  44. return year + '-' + month + '-' + day
  45. }else if(flag == 3){
  46. return month + '-' + day + ' ' + hour + ':' + minute;
  47. }else if( flag == 4) {
  48. return year + '年' + month + '月' + day + '日';
  49. }else if( flag == 5){
  50. return day.toString().substring(1,2)
  51. }
  52. }
  53. function GetQueryString(str,name){
  54. var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  55. var r = str.match(reg);
  56. if(r!=null) return unescape(r[2]);
  57. return null;
  58. }
  59. module.exports = {
  60. formatTime: formatTime,
  61. formatDate,
  62. GetQueryString,
  63. lastHours,
  64. lastHoursInGroup,
  65. addThreeYears
  66. }