filter.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import moment from 'moment'
  2. import Vue from 'vue'
  3. Vue.filter('moment', (date, type = 1) => {
  4. if(!date) {
  5. return ''
  6. }
  7. if (type === 1) {
  8. return moment(date).format('HH:mm')
  9. } else if (type === 2) {
  10. return moment(date).format('YYYY-MM-DD HH:mm')
  11. } else if (type === 3) {
  12. return moment(date).format('YYYY-MM-DD')
  13. } else if (type === 4) {
  14. return moment(date).format('HH:mm:ss')
  15. } else if (type === 5) {
  16. return moment(date).format('YYYY-MM-DD HH:mm:ss')
  17. } else if (type === 6) {
  18. const result = moment(date).format('M月DD日 Ah:mm')
  19. return result.includes('PM') ? result.replace('PM', '下午') : result.replace('AM', '上午')
  20. } else if (type === 7) {
  21. return moment(date).format('MM-DD HH:mm')
  22. } else if (type === 8) {
  23. return moment(date).format('MM-DD')
  24. }
  25. })
  26. Vue.filter('3days', function (dataStr, pattern = 'YYYY.MM.DD') {
  27. const current = moment()
  28. const time = moment(dataStr, 'YYYY-MM-DD HH:mm:ss')
  29. const diff = current.diff(time) / 1000
  30. if (diff >= (3 * 24 * 60 * 60)) {
  31. // 大于3天
  32. return time.format(pattern)
  33. } else if (diff < (3 * 24 * 60 * 60) && diff >= (24 * 60 * 60)) {
  34. return current.diff(time, 'days') + ' 天前'
  35. } else if (diff < (24 * 60 * 60) && diff >= (60 * 60)) {
  36. return current.diff(time, 'hours') + ' 小时前'
  37. } else if (diff < (60 * 60) && diff >= (60)) {
  38. return current.diff(time, 'minutes') + ' 分钟前'
  39. } else if (diff < (60) && diff >= (0)) {
  40. return current.diff(time, 'seconds') + ' 秒前'
  41. } else {
  42. return time.format(pattern)
  43. }
  44. })