| 5 |
stevensc |
1 |
const getYears = () => {
|
|
|
2 |
const date = new Date()
|
|
|
3 |
const currentYear = date.getFullYear()
|
|
|
4 |
let years = []
|
|
|
5 |
for (let index = currentYear; index > currentYear - 100; index--) {
|
|
|
6 |
years = [...years, index]
|
|
|
7 |
}
|
|
|
8 |
return years
|
|
|
9 |
}
|
|
|
10 |
|
|
|
11 |
const getMonths = () => {
|
|
|
12 |
const months = Array.from({ length: 12 }, (item, i) => {
|
|
|
13 |
return new Date(0, i).toLocaleString('es-ES', { month: 'long' })
|
|
|
14 |
})
|
|
|
15 |
|
|
|
16 |
return months
|
|
|
17 |
}
|
|
|
18 |
|
| 3021 |
stevensc |
19 |
export function getTimeDiff(segundos) {
|
|
|
20 |
const currentDate = new Date()
|
|
|
21 |
const futureDate = new Date(currentDate.getTime() + segundos * 1000)
|
|
|
22 |
const diff = futureDate - currentDate
|
|
|
23 |
|
|
|
24 |
const days = Math.floor(diff / (1000 * 60 * 60 * 24))
|
|
|
25 |
const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
|
|
|
26 |
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
|
|
|
27 |
|
|
|
28 |
return `${addZero(days)}d ${addZero(hours)}h ${addZero(minutes)}m`
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
function addZero(unit) {
|
|
|
32 |
return String(unit).padStart(2, '0')
|
|
|
33 |
}
|
|
|
34 |
|
| 5 |
stevensc |
35 |
const getMonthName = (monthNumber) => {
|
|
|
36 |
const date = new Date()
|
|
|
37 |
date.setMonth(monthNumber - 1)
|
|
|
38 |
|
|
|
39 |
const month = date.toLocaleString('es-ES', { month: 'long' })
|
|
|
40 |
|
|
|
41 |
return month
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
const DATE_UNITS = {
|
|
|
45 |
year: 31557600,
|
|
|
46 |
month: 2628000,
|
|
|
47 |
day: 86000,
|
|
|
48 |
hour: 3600,
|
|
|
49 |
minute: 60,
|
| 1064 |
stevensc |
50 |
second: 1
|
| 5 |
stevensc |
51 |
}
|
|
|
52 |
|
|
|
53 |
const rft = new Intl.RelativeTimeFormat('en', { numeric: 'auto' })
|
|
|
54 |
|
|
|
55 |
const getRelativeTime = (time) => {
|
|
|
56 |
const started = new Date(time + 1000).getTime()
|
|
|
57 |
const now = new Date().getTime()
|
|
|
58 |
|
|
|
59 |
const elapsed = (started - now) / 1000
|
|
|
60 |
|
|
|
61 |
for (const unit in DATE_UNITS) {
|
|
|
62 |
const absoluteElapsed = Math.abs(elapsed)
|
|
|
63 |
|
|
|
64 |
if (absoluteElapsed > DATE_UNITS[unit] || unit === 'second') {
|
|
|
65 |
return rft.format(Math.floor(elapsed / DATE_UNITS[unit]), unit)
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
return ''
|
|
|
70 |
}
|
|
|
71 |
|
| 1064 |
stevensc |
72 |
const formatDate = (date) => {
|
|
|
73 |
const dateObj = new Date(date)
|
|
|
74 |
|
|
|
75 |
if (isNaN(dateObj)) {
|
|
|
76 |
return date
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
return new Intl.DateTimeFormat('es', {
|
|
|
80 |
dateStyle: 'medium',
|
|
|
81 |
timeStyle: 'short',
|
|
|
82 |
timeZone: 'UTC',
|
|
|
83 |
hour12: true
|
|
|
84 |
}).format(dateObj)
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
export { getYears, getMonthName, getMonths, getRelativeTime, formatDate }
|