utils.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const REGEX_FORMAT = new RegExp("([^\]]+)|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS", "gi")
  2. export const formatDate = (value, format = 'YYYY-MM-DD') => {
  3. if (!value) return ''
  4. // ios 和 mac 系统中,带横杆的字符串日期是格式不了的,这里做一下判断处理
  5. if (typeof value === "string" && new Date(value).toString() === "Invalid Date") {
  6. value = value.replace(/-/g, "/");
  7. }
  8. const date = new Date(value)
  9. const year = date.getFullYear()
  10. const month = date.getMonth()
  11. const days = date.getDate()
  12. const day = date.getDay()
  13. const hours = date.getHours()
  14. const minutes = date.getMinutes()
  15. const seconds = date.getSeconds()
  16. const milliseconds = date.getMilliseconds()
  17. const matches = {
  18. YY: String(year).slice(-2),
  19. yy: String(year).slice(-2),
  20. YYYY: year,
  21. yyyy: year,
  22. M: month + 1,
  23. MM: `${month + 1}`.padStart(2, '0'),
  24. D: String(days),
  25. DD: `${days}`.padStart(2, '0'),
  26. H: String(hours),
  27. HH: `${hours}`.padStart(2, '0'),
  28. h: `${hours % 12 || 12}`.padStart(1, '0'),
  29. hh: `${hours % 12 || 12}`.padStart(2, '0'),
  30. m: String(minutes),
  31. mm: `${minutes}`.padStart(2, '0'),
  32. s: String(seconds),
  33. ss: `${seconds}`.padStart(2, '0'),
  34. SSS: `${milliseconds}`.padStart(3, '0'),
  35. d: day,
  36. }
  37. return format.replace(REGEX_FORMAT, (match, $1) => $1 || matches[match])
  38. }
  39. /**
  40. * 传入一个对象和布尔值,return根据ASCII码升序或者降序的对象
  41. * isSort: true代表升序,false代表降序
  42. * */
  43. export const sortASCII = (obj,isSort) => {
  44. let arr = []
  45. Object.keys(obj).forEach(item => arr.push(item))
  46. let sortArr = isSort ? arr.sort() : arr.sort().reverse()
  47. let sortObj = {}
  48. for (let i in sortArr) {
  49. sortObj[sortArr[i]] = obj[sortArr[i]]
  50. }
  51. return sortObj
  52. }