Repository navigation
Expand file tree
/
Copy pathindex.md
More file actions
97 lines (66 loc) · 3.29 KB
/
Copy pathindex.md
File metadata and controls
97 lines (66 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
---
title: Intl.RelativeTimeFormat
slug: Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat
---
{{JSRef}}
El objeto **`Intl.RelativeTimeFormat`** te proporciona una manera de formatear tiempos relativos con traducciones.
{{InteractiveExample("JavaScript Demo: Intl.RelativeTimeFormat")}}
```js interactive-example
const rtf1 = new Intl.RelativeTimeFormat("en", { style: "short" });
console.log(rtf1.format(3, "quarter"));
// Expected output: "in 3 qtrs."
console.log(rtf1.format(-1, "day"));
// Expected output: "1 day ago"
const rtf2 = new Intl.RelativeTimeFormat("es", { numeric: "auto" });
console.log(rtf2.format(2, "day"));
// Expected output: "pasado mañana"
```
## Constructor
- {{jsxref("Intl/RelativeTimeFormat/RelativeTimeFormat()", "Intl.RelativeTimeFormat.RelativeTimeFormat()")}}
- : Crea una nueva instancia de `Intl.RelativeTimeFormat`.
## Métodos estáticos
- {{jsxref("Intl/RelativeTimeFormat/supportedLocalesOf", "Intl.RelativeTimeFormat.supportedLocalesOf()")}}
- : Devuelve un {{jsxref("Array")}} con todos los idiomas disponibles sin necesidad de usar el que hay por defecto.
## Métodos de instancia
- {{jsxref("Intl/RelativeTimeFormat/format", "Intl.RelativeTimeFormat.prototype.format()")}}
- : Formatea `value` y `unit` conforme al idioma y las opciones de formateo al crear la instancia con [`Intl.RelativeTimeFormat`](/es/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat).
- {{jsxref("Intl/RelativeTimeFormat/formatToParts", "Intl.RelativeTimeFormat.prototype.formatToParts()")}}
- : Devuelve un {{jsxref("Array")}} de objetos representando el tiempo relativo en partes que pueden ser usadas en traducciones.
- {{jsxref("Intl/RelativeTimeFormat/resolvedOptions", "Intl.RelativeTimeFormat.prototype.resolvedOptions()")}}
- : Devuelve un nuevo objeto con las propiedades que reflejan las opciones de localización y formato usadas durante la inicialización del objeto.
## Ejemplos
### Ejemplo básico
El siguiente ejemplo muestra cómo conseguir el tiempo relativo para el mejor idioma según el usuario.
```js
// Crea un formateador de tiempo relativo en tu lenguaje
// con los valores por defectos pasados expresamente.
const rtf = new Intl.RelativeTimeFormat("en", {
localeMatcher: "best fit", // otros valores: "lookup"
numeric: "always", // otros valores: "auto"
style: "long", // otros valores: "short" or "narrow"
});
// Formatea el tiempo relativo con valores negativos (-1).
rtf.format(-1, "day");
// > "Hace 1 día"
// Formatea el tiempo relativo con valores positivos (1).
rtf.format(1, "day");
// > "Dentro de 1 día"
```
### Usando `formatToParts`
El siguiente ejemplo muestra cómo crear un formateador de tiempo relativo que devuelve las partes separadas:
```js
const rtf = new Intl.RelativeTimeFormat("es", { numeric: "auto" });
// Formatea el tiempo relativo usando día como unidad.
rtf.formatToParts(-1, "day");
// > [{ type: "literal", value: "ayer"}]
rtf.formatToParts(100, "day");
// > [{ type: "literal", value: "Dentro de " },
// > { type: "integer", value: "100", unit: "day" },
// > { type: "literal", value: " días" }]
```
## Especificaciones
{{Specifications}}
## Compatibilidad con navegadores
{{Compat}}
## Ver también
- [The Intl.RelativeTimeFormat API](https://developers.google.com/web/updates/2018/10/intl-relativetimeformat)