util.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 formatNumber = n => {
  11. n = n.toString()
  12. return n[1] ? n : '0' + n
  13. }
  14. //时间戳转时间
  15. function formatDate(time, flag) {
  16. console.log(time)
  17. const t = new Date(time);
  18. const tf = function(i){return (i < 10 ? '0' : '') + i};
  19. const year = t.getFullYear();
  20. const month = tf(t.getMonth() + 1);
  21. const day = tf(t.getDate());
  22. const hour = tf(t.getHours());
  23. const minute = tf(t.getMinutes());
  24. //console.log( month + '月' + day + '日' + hour + ':' + minute);
  25. if(flag == 1) {
  26. return month + '月' + day + '日' + ' ' + hour + ':' + minute;
  27. }else if(flag == 2) {
  28. console.log(year,month,day)
  29. return year + '-' + month + '-' + day
  30. }else if(flag == 3){
  31. return month + '-' + day + ' ' + hour + ':' + minute;
  32. }
  33. }
  34. function GetQueryString(str,name){
  35. var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  36. var r = str.match(reg);
  37. if(r!=null) return unescape(r[2]);
  38. return null;
  39. }
  40. module.exports = {
  41. formatTime: formatTime,
  42. formatDate,
  43. GetQueryString
  44. }