index.js 3.0 KB

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