filter.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import accounting from 'accounting'
  2. const formatRelative = function (value, ms = false) {
  3. let now = ms ? +new Date() : +new Date() / 1000
  4. let day, hour, minute, second, result
  5. let _value = value - now
  6. if (_value <= 0) {
  7. return '0天'
  8. }
  9. day = parseInt(_value / 86400)
  10. hour = parseInt(_value % 86400 / 3600)
  11. minute = parseInt(_value % 3600 / 60)
  12. second = parseInt(_value % 3600 % 60)
  13. result = day > 0 ? `${day}天` : hour > 0 ? `${hour}小时` : minute > 0 ? `${minute}分` : `${second}秒`
  14. return result
  15. }
  16. const formatTime = function (value) {
  17. let date = new Date(value * 1000)
  18. let year = date.getFullYear()
  19. let month = date.getMonth() + 1
  20. let day = date.getDate()
  21. let hour = date.getHours()
  22. let minute = date.getMinutes()
  23. return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
  24. }
  25. const formatChTime = function (value) {
  26. let date = new Date(value * 1000)
  27. let year = date.getFullYear()
  28. let month = date.getMonth() + 1
  29. let day = date.getDate()
  30. let hour = date.getHours()
  31. let minute = date.getMinutes()
  32. let second = date.getSeconds()
  33. return year + '年' + month + '月' + day + '日' + ' , ' + [hour, minute, second].map(formatNumber).join(':')
  34. }
  35. const formatHhTime = function (value) {
  36. let date = new Date(value * 1000)
  37. let year = date.getFullYear()
  38. let month = date.getMonth() + 1
  39. let day = date.getDate()
  40. return year + '年' + month + '月' + day + '日'
  41. }
  42. const formatSTime = function (value) {
  43. let date = new Date(value * 1000)
  44. let year = date.getFullYear()
  45. let month = date.getMonth() + 1
  46. let day = date.getDate()
  47. let hour = date.getHours()
  48. let minute = date.getMinutes()
  49. let second = date.getSeconds()
  50. return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  51. }
  52. const formatDate = function (value) {
  53. let date = new Date(value * 1000)
  54. let year = date.getFullYear()
  55. let month = date.getMonth() + 1
  56. let day = date.getDate()
  57. return [year, month, day].map(formatNumber).join('-')
  58. }
  59. const formatDDate = function (value) {
  60. let date = new Date(value * 1000)
  61. let year = date.getFullYear()
  62. let month = date.getMonth() + 1
  63. let day = date.getDate()
  64. return [year, month, day].map(formatNumber).join('.')
  65. }
  66. const formatNumber = function (n) {
  67. n = n.toString()
  68. return n[1] ? n : '0' + n
  69. }
  70. const format = (time) => {
  71. let ymd = ''
  72. let mouth = (time.getMonth() + 1) >= 10 ? (time.getMonth() + 1) : ('0' + (time.getMonth() + 1))
  73. let day = time.getDate() >= 10 ? time.getDate() : ('0' + time.getDate())
  74. ymd += time.getFullYear() + '-' // 获取年份。
  75. ymd += mouth + '-' // 获取月份。
  76. ymd += day // 获取日。
  77. return ymd // 返回日期。
  78. }
  79. export const getAllDate = (start, end) => {
  80. let dateArr = []
  81. let startArr = start.split('-')
  82. let endArr = end.split('-')
  83. let db = new Date()
  84. db.setUTCFullYear(startArr[0], startArr[1] - 1, startArr[2])
  85. let de = new Date()
  86. de.setUTCFullYear(endArr[0], endArr[1] - 1, endArr[2])
  87. let unixDb = db.getTime()
  88. let unixDe = de.getTime()
  89. let stamp
  90. const oneDay = 24 * 60 * 60 * 1000
  91. for (stamp = unixDb; stamp <= unixDe;) {
  92. dateArr.push(format(new Date(parseInt(stamp))))
  93. stamp = stamp + oneDay
  94. }
  95. return dateArr
  96. }
  97. const getAcounting = function (val, text = '¥', short = true, sym = ',') {
  98. let max = 1000000
  99. if (val >= max && short) {
  100. return text + parseFloat((val / max).toFixed(3)) + '万'
  101. } else {
  102. return accounting.formatMoney(val / 100, text, val % 100 ? 2 : 0, sym)
  103. }
  104. }
  105. const numFormat = function (num) {
  106. if (num >= 10000) {
  107. num = Math.round(num / 1000) / 10 + '万'
  108. return num
  109. } else {
  110. return num
  111. }
  112. }
  113. const timeTalk = function (val) {
  114. let result
  115. let time = Math.round(new Date().getTime() / 1000)
  116. if (time - val < 86400 && time - val > 3600) {
  117. result = Math.floor((time - val) / 3600)
  118. return result + '小时前'
  119. } else if (time - val <= 3600 && time - val > 60) {
  120. result = Math.floor((time - val) / 60)
  121. return result + '分钟前'
  122. } else if (time - val <= 60 && time - val > 0) {
  123. return '刚刚'
  124. } else {
  125. result = formatTime(val)
  126. return result
  127. }
  128. }
  129. export default {
  130. formatRelative,
  131. formatTime,
  132. formatDate,
  133. // getAcounting,
  134. timeTalk,
  135. formatSTime,
  136. getAllDate,
  137. format,
  138. getAcounting,
  139. formatDDate,
  140. formatChTime,
  141. numFormat,
  142. formatHhTime
  143. }