date-util.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * 时间格式
  3. */
  4. export function formatTime (time) {
  5. if (!time || time.length < 6) return ''
  6. let seconds = time.slice(-2)
  7. let minutes = time.slice(-4, -2)
  8. let hours = time.slice(0, -4)
  9. return hours + ':' + minutes + ':' + seconds
  10. }
  11. /**
  12. * 日期格式
  13. */
  14. export function formatDate (date) {
  15. if (!date || date.length < 6) return ''
  16. let day = date.slice(-2)
  17. let month = date.slice(-4, -2)
  18. let year = date.slice(0, -4)
  19. return year + '.' + month + '.' + day
  20. }
  21. /**
  22. * 北京时间
  23. * @param date
  24. */
  25. export function formatBeijingTime (date) {
  26. // let beijingTime = new Date(date.getTime() + 8 * 60 * 60 * 1000)
  27. // 格式化为 yyyy-MM-dd HH:mm:ss
  28. const year = date.getFullYear()
  29. const month = String(date.getMonth() + 1).padStart(2, '0') // 月份从0开始
  30. const day = String(date.getDate()).padStart(2, '0')
  31. const hours = String(date.getHours()).padStart(2, '0')
  32. const minutes = String(date.getMinutes()).padStart(2, '0')
  33. const seconds = String(date.getSeconds()).padStart(2, '0')
  34. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
  35. }
  36. export function getDate (dataTime) {
  37. if (!dataTime) return ''
  38. return dataTime.slice(0, 10)
  39. }