index.js 2.6 KB

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