formatTime.js 809 B

1234567891011121314151617181920212223
  1. //时间戳转时间
  2. function formatDate(time, flag) {
  3. const t = new Date(time);
  4. const tf = function(i){return (i < 10 ? '0' : '') + i};
  5. const year = t.getFullYear();
  6. const month = tf(t.getMonth() + 1);
  7. const day = tf(t.getDate());
  8. const hour = tf(t.getHours());
  9. const minute = tf(t.getMinutes());
  10. const seconds = tf(t.getSeconds());
  11. const map = new Map([
  12. [1, month + '月' + day + '日' + ' ' + hour + ':' + minute],
  13. [2, year + '-' + month + '-' + day],
  14. [3, month + '-' + day + ' ' + hour + ':' + minute],
  15. [4, year + '年' + month + '月' + day + '日'],
  16. [5, day.toString().substring(1,2)],
  17. [6, month + '.' + day],
  18. [7, year + '年' + month + '月' + day + '日' + hour + '时' + minute + '分' + seconds + '秒']
  19. ])
  20. return map.has(flag) && map.get(flag)
  21. }
  22. export default formatDate