Intl.DateTimeFormat.prototype.formatRangeToParts()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2017.
La fonction Intl.DateTimeFormat.prototype.formatRangeToParts()
permet, selon la locale utilisée, de représenter chaque élément de l'intervalle de dates via DateTimeFormat
.
Syntaxe
Intl.DateTimeFormat.prototype.formatRangeToParts(dateDebut, dateFin);
Exemples
Usage basique de formatRangeToParts
Cette fonction reçoit deux Dates et retourne un Array d'objets contenant les élements qui représentent chaque partie de l'intervalle de date formatée, selon la locale utilisée.
let date1 = new Date(Date.UTC(2007, 0, 10, 10, 0, 0));
let date2 = new Date(Date.UTC(2007, 0, 10, 11, 0, 0));
// > 'Wed, 10 Jan 2007 10:00:00 GMT'
// > 'Wed, 10 Jan 2007 11:00:00 GMT'
let fmt = new Intl.DateTimeFormat("en", {
hour: "numeric",
minute: "numeric",
});
console.log(fmt.formatRange(date1, date2));
// > '10:00 – 11:00 AM'
fmt.formatRangeToParts(date1, date2);
// retourne la valeur:
// [
// { type: 'hour', value: '10', source: "startRange" },
// { type: 'literal', value: ':', source: "startRange" },
// { type: 'minute', value: '00', source: "startRange" },
// { type: 'literal', value: ' – ', source: "shared" },
// { type: 'hour', value: '11', source: "endRange" },
// { type: 'literal', value: ':', source: "endRange" },
// { type: 'minute', value: '00', source: "endRange" },
// { type: 'literal', value: ' ', source: "shared" },
// { type: 'dayPeriod', value: 'AM', source: "shared" }
// ]
Spécifications
Specification |
---|
ECMAScript® 2025 Internationalization API Specification # sec-Intl.DateTimeFormat.prototype.formatRangeToParts |
Compatibilité des navigateurs
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
formatRangeToParts |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- See implementation notes.