Intl.DateTimeFormat.prototype.formatRange() 方法是 JavaScript 中的內置方法,用於根據實例化 Intl.DateTimeFormat 對象時提供的語言環境和選項以最簡潔的方式格式化日期範圍。
用法:
Intl.DateTimeFormat.prototype.formatRange(startDate, endDate)
參數:此方法接受上麵提到並在下麵描述的兩個參數:
- startDate:此參數保存範圍的開始日期。
- endDate:此參數保存範圍的結束日期。
下麵的例子說明了 JavaScript 中的 Intl.DateTimeFormat.prototype.formatRange() 方法:
範例1:
const geeks1 = { weekday:'long', year:'numeric',
month:'long', day:'numeric' };
const geeks2 = { year:'2-digit', month:'numeric',
day:'numeric' };
const startDate = new Date(Date.UTC(1997, 5, 30, 3, 3, 3));
const endDate = new Date(Date.UTC(2073, 7, 6, 11, 0, 0));
const dateTime = new Intl.DateTimeFormat('en', geeks1);
console.log(dateTime.formatRange(startDate, endDate));
const dateTime1 = new Intl.DateTimeFormat('hi', geeks1);
console.log(dateTime1.formatRange(startDate, endDate));
const dateTime2 = new Intl.DateTimeFormat('en', geeks2);
console.log(dateTime2.formatRange(startDate, endDate));
const dateTime3 = new Intl.DateTimeFormat('hi', geeks2);
console.log(dateTime3.formatRange(startDate, endDate));
輸出:
範例2:
let geek1 = new Date(Date.UTC(1997, 5, 30, 10, 0, 0));
let geek2 = new Date(Date.UTC(2020, 2, 26, 11, 0, 0));
let geek3 = new Date(Date.UTC(2073, 6, 23, 10, 0, 0));
let result1 = new Intl.DateTimeFormat("en", {
year:'2-digit',
month:'numeric',
day:'numeric',
hour:'numeric',
minute:'numeric'
});
console.log(result1.format(geek1));
console.log(result1.formatRange(geek1, geek2));
console.log(result1.formatRange(geek1, geek3));
let result2 = new Intl.DateTimeFormat("hi", {
year:'numeric',
month:'short',
day:'numeric'
});
console.log(result2.format(geek1));
console.log(result2.formatRange(geek1, geek2));
console.log(result2.formatRange(geek1, geek3));
輸出:
支持的瀏覽器: Intl.DateTimeFormat.prototype.formatRange() 方法支持的瀏覽器如下:
- 穀歌瀏覽器
- Firefox
- Opera
- Edge
- IE
- Safari
相關用法
- Javascript dataView.getInt16()用法及代碼示例
- Javascript RegExp toString()用法及代碼示例
- JavaScript Date toLocaleTimeString()用法及代碼示例
- JavaScript Math random()用法及代碼示例
- JavaScript Math round()用法及代碼示例
- Javascript toFixed()用法及代碼示例
注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 JavaScript | Intl.DateTimeFormat.prototype.formatRange() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。