util.js 771 B

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. const setDuration = (s) => {
  16. let t = '';
  17. s = Math.floor(s);
  18. if (s > -1) {
  19. let min = Math.floor(s / 60) % 60;
  20. let sec = s % 60;
  21. if (min < 10) {
  22. t += "0";
  23. }
  24. t += min + ":";
  25. if (sec < 10) {
  26. t += "0";
  27. }
  28. t += sec;
  29. }
  30. return t
  31. }
  32. module.exports = {
  33. formatTime,
  34. setDuration
  35. }