index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. function pluralize (time, label) {
  2. if (time === 1) {
  3. return time + label
  4. }
  5. return time + label + 's'
  6. }
  7. /**
  8. * 日期格式化
  9. */
  10. export function dateFormat (date) {
  11. let format = 'yyyy-MM-dd hh:mm:ss'
  12. if (date != 'Invalid Date') {
  13. var o = {
  14. 'M+': date.getMonth() + 1, // month
  15. 'd+': date.getDate(), // day
  16. 'h+': date.getHours(), // hour
  17. 'm+': date.getMinutes(), // minute
  18. 's+': date.getSeconds(), // second
  19. 'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
  20. 'S': date.getMilliseconds() // millisecond
  21. }
  22. if (/(y+)/.test(format)) {
  23. format = format.replace(RegExp.$1,
  24. (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  25. }
  26. for (var k in o) {
  27. if (new RegExp('(' + k + ')').test(format)) {
  28. format = format.replace(RegExp.$1,
  29. RegExp.$1.length == 1 ? o[k]
  30. : ('00' + o[k]).substr(('' + o[k]).length))
  31. }
  32. }
  33. return format
  34. }
  35. return ''
  36. }
  37. export function timeAgo (time) {
  38. const between = Date.now() / 1000 - Number(time)
  39. if (between < 3600) {
  40. return pluralize(~~(between / 60), ' minute')
  41. } else if (between < 86400) {
  42. return pluralize(~~(between / 3600), ' hour')
  43. } else {
  44. return pluralize(~~(between / 86400), ' day')
  45. }
  46. }
  47. export function parseTime (time, cFormat) {
  48. if (arguments.length === 0) {
  49. return null
  50. }
  51. if ((time + '').length === 10) {
  52. time = +time * 1000
  53. }
  54. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  55. let date
  56. if (typeof time === 'object') {
  57. date = time
  58. } else {
  59. date = new Date(parseInt(time))
  60. }
  61. const formatObj = {
  62. y: date.getFullYear(),
  63. m: date.getMonth() + 1,
  64. d: date.getDate(),
  65. h: date.getHours(),
  66. i: date.getMinutes(),
  67. s: date.getSeconds(),
  68. a: date.getDay()
  69. }
  70. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  71. let value = formatObj[key]
  72. if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
  73. if (result.length > 0 && value < 10) {
  74. value = '0' + value
  75. }
  76. return value || 0
  77. })
  78. return time_str
  79. }
  80. export function formatTime (time, option) {
  81. time = +time * 1000
  82. const d = new Date(time)
  83. const now = Date.now()
  84. const diff = (now - d) / 1000
  85. if (diff < 30) {
  86. return '刚刚'
  87. } else if (diff < 3600) { // less 1 hour
  88. return Math.ceil(diff / 60) + '分钟前'
  89. } else if (diff < 3600 * 24) {
  90. return Math.ceil(diff / 3600) + '小时前'
  91. } else if (diff < 3600 * 24 * 2) {
  92. return '1天前'
  93. }
  94. if (option) {
  95. return parseTime(time, option)
  96. } else {
  97. return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分'
  98. }
  99. }
  100. /* 数字 格式化 */
  101. export function nFormatter (num, digits) {
  102. const si = [
  103. { value: 1E18, symbol: 'E' },
  104. { value: 1E15, symbol: 'P' },
  105. { value: 1E12, symbol: 'T' },
  106. { value: 1E9, symbol: 'G' },
  107. { value: 1E6, symbol: 'M' },
  108. { value: 1E3, symbol: 'k' }
  109. ]
  110. for (let i = 0; i < si.length; i++) {
  111. if (num >= si[i].value) {
  112. return (num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
  113. }
  114. }
  115. return num.toString()
  116. }
  117. export function html2Text (val) {
  118. const div = document.createElement('div')
  119. div.innerHTML = val
  120. return div.textContent || div.innerText
  121. }
  122. export function toThousandslsFilter (num) {
  123. return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
  124. }