formatTime.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import Vue from 'vue'
  2. Vue.filter('formatTime', function (thistime, fmt = 'yyyy-MM-dd hh:mm:ss') {
  3. let $this = new Date(thistime)
  4. let o = {
  5. 'M+': $this.getMonth() + 1,
  6. 'd+': $this.getDate(),
  7. 'h+': $this.getHours(),
  8. 'm+': $this.getMinutes(),
  9. 's+': $this.getSeconds(),
  10. 'q+': Math.floor(($this.getMonth() + 3) / 3),
  11. 'S': $this.getMilliseconds()
  12. }
  13. if (/(y+)/.test(fmt)) {
  14. fmt = fmt.replace(RegExp.$1, ($this.getFullYear() + '').substr(4 - RegExp.$1.length))
  15. }
  16. for (var k in o) {
  17. if (new RegExp('(' + k + ')').test(fmt)) {
  18. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  19. }
  20. }
  21. return fmt
  22. })
  23. Vue.prototype.formatTime = function (thistime,fmt = 'yyyy-MM-dd hh:mm:ss') {
  24. let $this = new Date(thistime)
  25. let o = {
  26. 'M+': $this.getMonth() + 1,
  27. 'd+': $this.getDate(),
  28. 'h+': $this.getHours(),
  29. 'm+': $this.getMinutes(),
  30. 's+': $this.getSeconds(),
  31. 'q+': Math.floor(($this.getMonth() + 3) / 3),
  32. 'S': $this.getMilliseconds()
  33. }
  34. if (/(y+)/.test(fmt)) {
  35. fmt = fmt.replace(RegExp.$1, ($this.getFullYear() + '').substr(4 - RegExp.$1.length))
  36. }
  37. for (var k in o) {
  38. if (new RegExp('(' + k + ')').test(fmt)) {
  39. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  40. }
  41. }
  42. return fmt
  43. }