天波用户运营管理后台系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
3.4KB

  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 {number} time
  93. * @param {string} option
  94. * @returns {string}
  95. */
  96. export function initTime(time, option) {
  97. console.log("option:;", option);
  98. const year = time.getFullYear();
  99. const month = time.getMonth() + 1;
  100. const day = time.getDate();
  101. const hours = time.getHours();
  102. const minutes = time.getMinutes();
  103. if(option === 'ymdhm') {
  104. return year + '年' + month + '月' + day + '日' + hours + '时' + minutes +'分';
  105. } else if (option === 'ymdh') {
  106. return year + '年' + month + '月' + day + '日' + hours + '时';
  107. } else if (option === 'ymd') {
  108. return year + '年' + month + '月' + day + '日';
  109. } else if (option === 'ym') {
  110. return year + '年' + month + '月';
  111. }
  112. // year + '年' + month + '月' + day + '日' + hours + '时';
  113. }
  114. /**
  115. * @param {string} url
  116. * @returns {Object}
  117. */
  118. export function param2Obj(url) {
  119. const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  120. if (!search) {
  121. return {}
  122. }
  123. const obj = {}
  124. const searchArr = search.split('&')
  125. searchArr.forEach(v => {
  126. const index = v.indexOf('=')
  127. if (index !== -1) {
  128. const name = v.substring(0, index)
  129. const val = v.substring(index + 1, v.length)
  130. obj[name] = val
  131. }
  132. })
  133. return obj
  134. }