util.ts 650 B

12345678910111213141516171819202122232425262728
  1. const dayjs = require("dayjs")
  2. const formatTime = (date: Date) => {
  3. const year = date.getFullYear()
  4. const month = date.getMonth() + 1
  5. const day = date.getDate()
  6. const hour = date.getHours()
  7. const minute = date.getMinutes()
  8. const second = date.getSeconds()
  9. return (
  10. [year, month, day].map(formatNumber).join('/') +
  11. ' ' +
  12. [hour, minute, second].map(formatNumber).join(':')
  13. )
  14. }
  15. const formatNumber = (n: number) => {
  16. const s = n.toString()
  17. return s[1] ? s : '0' + s
  18. }
  19. const convertTime = (time: string, type?: string)=>{
  20. return dayjs(time).format(type ? type : 'YYYY-MM-DD');
  21. }
  22. export {
  23. formatTime,
  24. convertTime
  25. }