time-util.vue 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <script>
  2. function renderTime(date) {
  3. if (date === '' || date == null) {
  4. return ''
  5. }
  6. const da = new Date(date)
  7. return da.getFullYear() + '-' + (da.getMonth() + 1) + '-' + da.getDate() + ' ' + da.getHours() + ':' + da.getMinutes() + ':' + da.getSeconds()
  8. }
  9. function formateTimeToChinese(date) {
  10. if (date === '' || date == null) {
  11. return ''
  12. }
  13. const da = new Date(date)
  14. return da.getFullYear() + '年' + (da.getMonth() + 1) + '月' + da.getDate() + '日 ' + da.getHours() + '时' + da.getMinutes() + '分'
  15. }
  16. function formateTime(strat, end) {
  17. return formateTimeToChinese(strat) + ' ~ ' + formateTimeToChinese(end)
  18. }
  19. /**
  20. 视频发布时间距当前时间间隔
  21. */
  22. function timeToNowStrning(date) {
  23. const now = new Date().getTime()
  24. let t = now - date
  25. t = Math.trunc(t / 1000)
  26. if (t < 60) {
  27. return t + '秒前'
  28. }
  29. t = Math.trunc(t / 60)
  30. if (t < 60) {
  31. return t + '分钟前'
  32. }
  33. t = Math.trunc(t / 60)
  34. if (t < 24) {
  35. return t + '小时前'
  36. }
  37. t = Math.trunc(t / 24)
  38. if (t < 30) {
  39. return t + '天前'
  40. }
  41. return renderTime(date)
  42. }
  43. export default {
  44. timeToNowStrning,
  45. renderTime,
  46. formateTimeToChinese,
  47. formateTime
  48. }
  49. </script>