logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

date_utils.js (1664B)


  1. export const SECOND = 1000
  2. export const MINUTE = 60 * SECOND
  3. export const HOUR = 60 * MINUTE
  4. export const DAY = 24 * HOUR
  5. export const WEEK = 7 * DAY
  6. export const MONTH = 30 * DAY
  7. export const YEAR = 365.25 * DAY
  8. export const relativeTime = (date, nowThreshold = 1) => {
  9. if (typeof date === 'string') date = Date.parse(date)
  10. const round = Date.now() > date ? Math.floor : Math.ceil
  11. const d = Math.abs(Date.now() - date)
  12. const r = { num: round(d / YEAR), key: 'time.unit.years' }
  13. if (d < nowThreshold * SECOND) {
  14. r.num = 0
  15. r.key = 'time.now'
  16. } else if (d < MINUTE) {
  17. r.num = round(d / SECOND)
  18. r.key = 'time.unit.seconds'
  19. } else if (d < HOUR) {
  20. r.num = round(d / MINUTE)
  21. r.key = 'time.unit.minutes'
  22. } else if (d < DAY) {
  23. r.num = round(d / HOUR)
  24. r.key = 'time.unit.hours'
  25. } else if (d < WEEK) {
  26. r.num = round(d / DAY)
  27. r.key = 'time.unit.days'
  28. } else if (d < MONTH) {
  29. r.num = round(d / WEEK)
  30. r.key = 'time.unit.weeks'
  31. } else if (d < YEAR) {
  32. r.num = round(d / MONTH)
  33. r.key = 'time.unit.months'
  34. }
  35. return r
  36. }
  37. export const relativeTimeShort = (date, nowThreshold = 1) => {
  38. const r = relativeTime(date, nowThreshold)
  39. r.key += '_short'
  40. return r
  41. }
  42. export const unitToSeconds = (unit, amount) => {
  43. switch (unit) {
  44. case 'minutes': return 0.001 * amount * MINUTE
  45. case 'hours': return 0.001 * amount * HOUR
  46. case 'days': return 0.001 * amount * DAY
  47. }
  48. }
  49. export const secondsToUnit = (unit, amount) => {
  50. switch (unit) {
  51. case 'minutes': return (1000 * amount) / MINUTE
  52. case 'hours': return (1000 * amount) / HOUR
  53. case 'days': return (1000 * amount) / DAY
  54. }
  55. }