util.js 1018 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. const formatSeconds = theTime => {
  15. let theTime1 = 0
  16. let theTime2 = 0
  17. if (theTime > 60) {
  18. theTime1 = parseInt(theTime / 60)
  19. theTime = parseInt(theTime % 60)
  20. if (theTime1 > 60) {
  21. theTime2 = parseInt(theTime1 / 60)
  22. theTime1 = parseInt(theTime1 % 60)
  23. }
  24. }
  25. let result = '' + parseInt(theTime) + '秒'
  26. if (theTime1 > 0) {
  27. result = '' + parseInt(theTime1) + '分' + result
  28. }
  29. if (theTime2 > 0) {
  30. result = '' + parseInt(theTime2) + '小时' + result
  31. }
  32. return result
  33. }
  34. module.exports = {
  35. formatSeconds: formatSeconds,
  36. formatTime: formatTime
  37. }