index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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') {
  48. return ['日', '一', '二', '三', '四', '五', '六'][value]
  49. }
  50. return value.toString().padStart(2, '0')
  51. })
  52. return time_str
  53. }
  54. /**
  55. * @param {number} time
  56. * @param {string} option
  57. * @returns {string}
  58. */
  59. export function formatTime(time, option) {
  60. if (('' + time).length === 10) {
  61. time = parseInt(time) * 1000
  62. } else {
  63. time = +time
  64. }
  65. const d = new Date(time)
  66. const now = Date.now()
  67. const diff = (now - d) / 1000
  68. if (diff < 30) {
  69. return '刚刚'
  70. } else if (diff < 3600) {
  71. // less 1 hour
  72. return Math.ceil(diff / 60) + '分钟前'
  73. } else if (diff < 3600 * 24) {
  74. return Math.ceil(diff / 3600) + '小时前'
  75. } else if (diff < 3600 * 24 * 2) {
  76. return '1天前'
  77. }
  78. if (option) {
  79. return parseTime(time, option)
  80. } else {
  81. return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分'
  82. }
  83. }
  84. /**
  85. * @param {string} url
  86. * @returns {Object}
  87. */
  88. export function param2Obj(url) {
  89. const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  90. if (!search) {
  91. return {}
  92. }
  93. const obj = {}
  94. const searchArr = search.split('&')
  95. searchArr.forEach(v => {
  96. const index = v.indexOf('=')
  97. if (index !== -1) {
  98. const name = v.substring(0, index)
  99. const val = v.substring(index + 1, v.length)
  100. obj[name] = val
  101. }
  102. })
  103. return obj
  104. }