| 3719 |
stevensc |
1 |
export 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 |
export 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 |
|
|
|
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 |
|
|
|
35 |
export 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,
|
|
|
50 |
second: 1
|
|
|
51 |
};
|
|
|
52 |
|
|
|
53 |
const rft = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
|
|
|
54 |
|
|
|
55 |
export 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 |
|
|
|
72 |
export const formatDate = (date) => {
|
| 3741 |
stevensc |
73 |
const now = new Date();
|
| 3719 |
stevensc |
74 |
|
| 3741 |
stevensc |
75 |
if (!date || isNaN(new Date(date))) {
|
|
|
76 |
return now.toLocaleString('es-ES', {
|
|
|
77 |
dateStyle: 'medium',
|
|
|
78 |
timeStyle: 'short',
|
|
|
79 |
timeZone: 'UTC',
|
|
|
80 |
hour12: true
|
|
|
81 |
});
|
| 3719 |
stevensc |
82 |
}
|
|
|
83 |
|
|
|
84 |
return new Intl.DateTimeFormat('es', {
|
|
|
85 |
dateStyle: 'medium',
|
|
|
86 |
timeStyle: 'short',
|
|
|
87 |
timeZone: 'UTC',
|
|
|
88 |
hour12: true
|
| 3741 |
stevensc |
89 |
}).format(new Date(date));
|
| 3719 |
stevensc |
90 |
};
|