index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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}
  9. */
  10. import dayjs from 'dayjs'
  11. export function parseTime (time, cFormat) {
  12. if (arguments.length === 0) {
  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') && (/^[0-9]+$/.test(time))) {
  21. time = parseInt(time)
  22. }
  23. if ((typeof time === 'number') && (time.toString().length === 10)) {
  24. time = time * 1000
  25. }
  26. date = new Date(time)
  27. }
  28. const formatObj = {
  29. y: date.getFullYear(),
  30. m: date.getMonth() + 1,
  31. d: date.getDate(),
  32. h: date.getHours(),
  33. i: date.getMinutes(),
  34. s: date.getSeconds(),
  35. a: date.getDay()
  36. }
  37. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  38. let value = formatObj[key]
  39. // Note: getDay() returns 0 on Sunday
  40. if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
  41. if (result.length > 0 && value < 10) {
  42. value = '0' + value
  43. }
  44. return value || 0
  45. })
  46. return time_str
  47. }
  48. /**
  49. * @param {number} time
  50. * @param {string} option
  51. * @returns {string}
  52. */
  53. export function formatTime (time, option) {
  54. if (('' + time).length === 10) {
  55. time = parseInt(time) * 1000
  56. } else {
  57. time = +time
  58. }
  59. const d = new Date(time)
  60. const now = Date.now()
  61. const diff = (now - d) / 1000
  62. if (diff < 30) {
  63. return '刚刚'
  64. } else if (diff < 3600) {
  65. // less 1 hour
  66. return Math.ceil(diff / 60) + '分钟前'
  67. } else if (diff < 3600 * 24) {
  68. return Math.ceil(diff / 3600) + '小时前'
  69. } else if (diff < 3600 * 24 * 2) {
  70. return '1天前'
  71. }
  72. if (option) {
  73. return parseTime(time, option)
  74. } else {
  75. return (
  76. d.getMonth() +
  77. 1 +
  78. '月' +
  79. d.getDate() +
  80. '日' +
  81. d.getHours() +
  82. '时' +
  83. d.getMinutes() +
  84. '分'
  85. )
  86. }
  87. }
  88. /**
  89. * @param {string} url
  90. * @returns {Object}
  91. */
  92. export function param2Obj (url) {
  93. const search = url.split('?')[1]
  94. if (!search) {
  95. return {}
  96. }
  97. return JSON.parse(
  98. '{"' +
  99. decodeURIComponent(search)
  100. .replace(/"/g, '\\"')
  101. .replace(/&/g, '","')
  102. .replace(/=/g, '":"')
  103. .replace(/\+/g, ' ') +
  104. '"}'
  105. )
  106. }
  107. export function countDown (time) {
  108. var s = 0;
  109. var hour = time.split(':')[0];
  110. var min = time.split(':')[1];
  111. var sec = time.split(':')[2];
  112. s = Number(hour * 3600) + Number(min * 60) + Number(sec);
  113. return s;
  114. }
  115. export function formatDuring (mss) {
  116. var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  117. var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
  118. var seconds = (mss % (1000 * 60)) / 1000;
  119. hours = hours < 10 ? ('0' + hours) : hours;
  120. minutes = minutes < 10 ? ('0' + minutes) : minutes;
  121. seconds = seconds < 10 ? ('0' + seconds) : seconds;
  122. return hours + ":" + minutes + ":" + seconds;
  123. }
  124. export const objectToOptions = data => {
  125. const options = []
  126. for (const key in data) {
  127. if (data.hasOwnProperty(key)) {
  128. const item = data[key]
  129. const upkey = key.toLocaleUpperCase()
  130. options.push({
  131. label: item,
  132. value: (upkey === 'TRUE' || upkey === 'FALSE' ? upkey === 'TRUE' : key)
  133. })
  134. }
  135. }
  136. return options
  137. }
  138. export const getTimes = (times, keys = [], format = 'YYYY-MM-DD') => {
  139. if (times && times.length) {
  140. return {
  141. [keys[0] || 'start']: dayjs(times[0]).isValid() ? dayjs(times[0]).format(format) : '',
  142. [keys[1] || 'end']: dayjs(times[1]).isValid() ? dayjs(times[1]).format(format) : '',
  143. }
  144. }
  145. return {}
  146. }