util.js 1.3 KB

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