Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3021 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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