aboutsummaryrefslogtreecommitdiff
path: root/files/es/web/javascript/reference/global_objects/intl/relativetimeformat/index.html
blob: e27304e97895a89f4165401e13ef6b65bfeb3848 (plain)
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
98
99
100
101
102
103
104
105
106
107
---
title: Intl.RelativeTimeFormat
slug: Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat
tags:
  - RelatimeTimeFormat
translation_of: Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat
original_slug: Web/JavaScript/Referencia/Objetos_globales/Intl/RelativeTimeFormat
---
<div>{{JSRef}}</div>

<p>El objeto <strong><code>Intl.RelativeTimeFormat</code></strong> te proporciona una manera de formatear tiempos relativos con traducciones.</p>

<div>{{EmbedInteractiveExample("pages/js/intl-relativetimeformat.html")}}</div>

<p class="hidden">El código de este ejemplo interactivo está disponible en un repositorio GitHub. Si quieres contribuir a los ejemplos interactivos del proyecto, por favor, clona <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> y manda una Pull Request.</p>

<h2 id="Constructor">Constructor</h2>

<dl>
 <dt>{{jsxref("RelativeTimeFormat.RelativeTimeFormat()", "Intl.RelativeTimeFormat.RelativeTimeFormat()")}}</dt>
 <dd>Crea una nueva instancia de <code>Intl.RelativeTimeFormat</code>.</dd>
</dl>

<h2 id="Métodos_estáticos">Métodos estáticos</h2>

<dl>
 <dt>{{jsxref("RelativeTimeFormat.supportedLocalesOf", "Intl.RelativeTimeFormat.supportedLocalesOf()")}}</dt>
 <dd>Devuelve un {{jsxref("Array")}} con todos los idiomas disponibles sin necesidad de usar el que hay por defecto.</dd>
</dl>

<h2 id="Métodos_de_instancia">Métodos de instancia</h2>

<dl>
 <dt>{{jsxref("RelativeTimeFormat.format", "Intl.RelativeTimeFormat.prototype.format()")}}</dt>
 <dd>Formatea <code>value</code> y <code>unit</code> conforme al idioma y las opciones de formateo al crear la instancia con <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl.RelativeTimeFormat"><code>Intl.RelativeTimeFormat</code></a>.</dd>
 <dt>{{jsxref("RelativeTimeFormat.formatToParts", "Intl.RelativeTimeFormat.prototype.formatToParts()")}}</dt>
 <dd>Devuelve un {{jsxref("Array")}} de objetos representando el tiempo relativo en partes que pueden ser usadas en traducciones.</dd>
 <dt>{{jsxref("RelativeTimeFormat.resolvedOptions", "Intl.RelativeTimeFormat.prototype.resolvedOptions()")}}</dt>
 <dd>Devuelve un nuevo objeto con las propiedades que reflejan las opciones de localización y formato usadas durante la inicialización del objeto.</dd>
</dl>

<h2 id="Ejemplos">Ejemplos</h2>

<h3 id="Ejemplo_básico">Ejemplo básico</h3>

<p>El siguiente ejemplo muestra cómo conseguir el tiempo relativo para el mejor idioma según el usuario.</p>

<pre class="brush: js notranslate">// 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");
// &gt; "Hace 1 día"

// Formatea el tiempo relativo con valores positivos (1).
rtf.format(1, "day");
// &gt; "Dentro de 1 día"</pre>

<h3 id="Usando_formatToParts">Usando <code>formatToParts</code></h3>

<p>El siguiente ejemplo muestra cómo crear un formateador de tiempo relativo que devuelve las partes separadas:</p>

<pre class="brush: js notranslate">const rtf = new Intl.RelativeTimeFormat("es", { numeric: "auto" });

// Formatea el tiempo relativo usando día como unidad.
rtf.formatToParts(-1, "day");
// &gt; [{ type: "literal", value: "ayer"}]

rtf.formatToParts(100, "day");
// &gt; [{ type: "literal", value: "Dentro de " },
// &gt;  { type: "integer", value: "100", unit: "day" },
// &gt;  { type: "literal", value: " días" }]
</pre>

<h2 id="Especificaciones">Especificaciones</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Especificación</th>
   <th scope="col">Estado</th>
   <th scope="col">Comentario</th>
  </tr>
  <tr>
   <td>{{SpecName('ES Int Draft', '#relativetimeformat-objects', 'RelativeTimeFormat')}}</td>
   <td></td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="Compatibilidad_en_navegadores">Compatibilidad en navegadores</h2>



<p>{{Compat("javascript.builtins.Intl.RelativeTimeFormat")}}</p>

<h2 id="Ver_también">Ver también</h2>

<ul>
 <li><a href="https://developers.google.com/web/updates/2018/10/intl-relativetimeformat">The Intl.RelativeTimeFormat API</a></li>
</ul>