index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Created by PanJiaChen on 16/11/18.
  3. */
  4. /**
  5. * Parse the time to string
  6. *
  7. * @param {(Object|string|number)} time
  8. * @param {string} cFormat
  9. * @returns {string | null}
  10. */
  11. export function parseTime(time, cFormat) {
  12. if (arguments.length === 0 || !time) {
  13. return null
  14. }
  15. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  16. let date
  17. if (typeof time === 'object') {
  18. date = time
  19. } else {
  20. if ((typeof time === 'string')) {
  21. if ((/^[0-9]+$/.test(time))) {
  22. // support "1548221490638"
  23. time = parseInt(time)
  24. } else {
  25. // support safari
  26. // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
  27. time = time.replace(new RegExp(/-/gm), '/')
  28. }
  29. }
  30. if ((typeof time === 'number') && (time.toString().length === 10)) {
  31. time = time * 1000
  32. }
  33. date = new Date(time)
  34. }
  35. const formatObj = {
  36. y: date.getFullYear(),
  37. m: date.getMonth() + 1,
  38. d: date.getDate(),
  39. h: date.getHours(),
  40. i: date.getMinutes(),
  41. s: date.getSeconds(),
  42. a: date.getDay()
  43. }
  44. const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
  45. const value = formatObj[key]
  46. // Note: getDay() returns 0 on Sunday
  47. if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
  48. return value.toString().padStart(2, '0')
  49. })
  50. return time_str
  51. }
  52. /**
  53. * @param {number} time
  54. * @param {string} option
  55. * @returns {string}
  56. */
  57. export function formatTime(time, option) {
  58. if (('' + time).length === 10) {
  59. time = parseInt(time) * 1000
  60. } else {
  61. time = +time
  62. }
  63. const d = new Date(time)
  64. const now = Date.now()
  65. const diff = (now - d) / 1000
  66. if (diff < 30) {
  67. return '刚刚'
  68. } else if (diff < 3600) {
  69. // less 1 hour
  70. return Math.ceil(diff / 60) + '分钟前'
  71. } else if (diff < 3600 * 24) {
  72. return Math.ceil(diff / 3600) + '小时前'
  73. } else if (diff < 3600 * 24 * 2) {
  74. return '1天前'
  75. }
  76. if (option) {
  77. return parseTime(time, option)
  78. } else {
  79. return (
  80. d.getMonth() +
  81. 1 +
  82. '月' +
  83. d.getDate() +
  84. '日' +
  85. d.getHours() +
  86. '时' +
  87. d.getMinutes() +
  88. '分'
  89. )
  90. }
  91. }
  92. /**
  93. * @param {string} url
  94. * @returns {Object}
  95. */
  96. export function param2Obj(url) {
  97. const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  98. if (!search) {
  99. return {}
  100. }
  101. const obj = {}
  102. const searchArr = search.split('&')
  103. searchArr.forEach(v => {
  104. const index = v.indexOf('=')
  105. if (index !== -1) {
  106. const name = v.substring(0, index)
  107. const val = v.substring(index + 1, v.length)
  108. obj[name] = val
  109. }
  110. })
  111. return obj
  112. }