util.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. const formatTime = date => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  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. return year + '-' + month + '-' + day
  28. }else if(flag == 3){
  29. return month + '-' + day + ' ' + hour + ':' + minute;
  30. }
  31. }
  32. module.exports = {
  33. formatTime: formatTime,
  34. formatDate
  35. }