time.js 864 B

12345678910111213141516171819202122232425262728293031323334
  1. // 时间戳转格式化时间
  2. function timestr(time, format = 'yyyy-MM-dd HH:mm:ss') {
  3. var t = new Date(time);
  4. var tf = function (i) {
  5. return (i < 10 ? '0' : '') + i
  6. };
  7. return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
  8. switch (a) {
  9. case 'yyyy':
  10. return tf(t.getFullYear());
  11. break;
  12. case 'MM':
  13. return tf(t.getMonth() + 1);
  14. break;
  15. case 'mm':
  16. return tf(t.getMinutes());
  17. break;
  18. case 'dd':
  19. return tf(t.getDate());
  20. break;
  21. case 'HH':
  22. return tf(t.getHours());
  23. break;
  24. case 'ss':
  25. return tf(t.getSeconds());
  26. break;
  27. }
  28. })
  29. }
  30. module.exports = {
  31. timestr
  32. }