util.js 923 B

123456789101112131415161718192021222324252627282930313233
  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 formatDateNotSecond = isoString => {
  11. const date = new Date(isoString);
  12. const year = date.getFullYear();
  13. const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are zero-based
  14. const day = String(date.getDate()).padStart(2, '0');
  15. const hours = String(date.getHours()).padStart(2, '0');
  16. const minutes = String(date.getMinutes()).padStart(2, '0');
  17. return `${year}-${month}-${day} ${hours}:${minutes}`;
  18. }
  19. const formatNumber = n => {
  20. n = n.toString()
  21. return n[1] ? n : `0${n}`
  22. }
  23. module.exports = {
  24. formatTime,
  25. formatDateNotSecond
  26. }