util.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. }else if( flag == 6) {
  52. return month + '.' + day ;
  53. }
  54. }
  55. function GetQueryString(str,name){
  56. var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  57. var r = str.match(reg);
  58. if(r!=null) return unescape(r[2]);
  59. return null;
  60. }
  61. module.exports = {
  62. formatTime: formatTime,
  63. formatDate,
  64. GetQueryString,
  65. lastHours,
  66. lastHoursInGroup,
  67. addThreeYears
  68. }