JS實(shí)現(xiàn)Date日期格式的本地化的方法小結(jié)
引言
為了更好的更新多語言日期的顯示,所以希望實(shí)現(xiàn)日期的本地化格式顯示要求,常規(guī)的特殊字符型格式化無法滿足顯示要求,這里整理了幾種我思考實(shí)現(xiàn)的本地化實(shí)現(xiàn)功能。
通過多方查找,總結(jié)了實(shí)現(xiàn)的思路主要有如下三個(gè)方向:
- 官方基礎(chǔ)支持:
javascript自支持Intl.DateTimeFormat實(shí)現(xiàn)本地化 - 三方工具:如
dayjs使用‘dayjs/plugin/localeData’ - 自己實(shí)現(xiàn)
DateTimeFormat實(shí)現(xiàn)本地化
JavaScript已經(jīng)提供了可以使用的本地化功能:Intl.DateTimeFormat,只需要傳入當(dāng)前語言和日期基本可以完成本地化的輸出,如下給出一些基礎(chǔ)的實(shí)現(xiàn):
const date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// 假定下面輸出的結(jié)果使用了洛杉磯時(shí)區(qū)(UTC-0800,太平洋標(biāo)準(zhǔn)時(shí)間)
// 美式英語 (US English) 使用 month-day-year 格式
console.log(new Intl.DateTimeFormat("en-US").format(date));
// "12/19/2012"
// 英式英語 (British English) 使用 day-month-year 格式
console.log(new Intl.DateTimeFormat("en-GB").format(date));
// "19/12/2012"
// 韓國使用 year-month-day 格式
console.log(new Intl.DateTimeFormat("ko-KR").format(date));
// "2012. 12. 19."
// 大部分阿拉伯國家使用阿拉伯字母 (real Arabic digits)
console.log(new Intl.DateTimeFormat("ar-EG").format(date));
// "???/???/????"
// 在日本,應(yīng)用可能想要使用日本日歷,
// 2012 年是平成 24 年(平成是是日本天皇明仁的年號,由 1989 年 1 月 8 日起開始計(jì)算直至現(xiàn)在)
console.log(new Intl.DateTimeFormat("ja-JP-u-ca-japanese").format(date));
// "24/12/19"
// 當(dāng)請求可能不支持的語言,如巴厘語(ban)時(shí),若同時(shí)指定了備用的語言,
// 那么將使用備用的語言輸出(本例為印尼語(id))
console.log(new Intl.DateTimeFormat(["ban", "id"]).format(date));
// "19/12/2012"同時(shí),提供給我們使用options進(jìn)行格式化的返回,這個(gè)很大程度已經(jīng)足夠使用了:
const date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// 請求參數(shù) (options) 中包含參數(shù)星期 (weekday),并且該參數(shù)的值為長類型 (long)
let options = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
};
console.log(new Intl.DateTimeFormat("de-DE", options).format(date));
// "Donnerstag, 20. Dezember 2012"
// 應(yīng)用可能需要使用世界標(biāo)準(zhǔn)時(shí)間 (UTC),并且 UTC 使用短名字 (short) 展示
options.timeZone = "UTC";
options.timeZoneName = "short";
console.log(new Intl.DateTimeFormat("en-US", options).format(date));
// "Thursday, December 20, 2012, GMT"
// 有時(shí)需要更精確的選項(xiàng)
options = {
hour: "numeric",
minute: "numeric",
second: "numeric",
timeZone: "Australia/Sydney",
timeZoneName: "short",
};
console.log(new Intl.DateTimeFormat("en-AU", options).format(date));
// "2:00:00 pm AEDT"
// 再精確些...
options.fractionalSecondDigits = 3; // 秒數(shù)的有效數(shù)字?jǐn)?shù)量
console.log(new Intl.DateTimeFormat("en-AU", options).format(date));
// "2:00:00.200 pm AEDT"
// 即便是美國,有時(shí)也需要使用 24 小時(shí)制
options = {
year: "numeric",
month: "numeric",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric",
hour12: false,
timeZone: "America/Los_Angeles",
};
console.log(new Intl.DateTimeFormat("en-US", options).format(date));
// "12/19/2012, 19:00:00"
// 要使用選項(xiàng),但是需要使用瀏覽器的默認(rèn)區(qū)域,請使用 'default'
console.log(new Intl.DateTimeFormat("default", options).format(date));
// "12/19/2012, 19:00:00"
// 有時(shí)需要包含一天的時(shí)段
options = { hour: "numeric", dayPeriod: "short" };
console.log(new Intl.DateTimeFormat("en-US", options).format(date));
// 10 at night將其封裝成方法如下:
function formatLocalTime(date) {
const options = {
year: 'numeric',
month: 'long',
}
// 我這里將lang寫在html標(biāo)簽進(jìn)行全局切換
const formatter = new Intl.DateTimeFormat(document.documentElement.lang, options)
return formatter.format(date)
}
const date = new Date()
formatLocalTime(date) // 2024年3月三方庫提供的本地化
其他的日期庫沒了解,這里介紹dayjs庫使用的本地化操作,dayjs自生無法直接進(jìn)行本地化,是需要通過插件dayjs/plugin/localeData來配合實(shí)現(xiàn)的。
1、安裝
$ npm install dayjs $ npm install dayjs/plugin/localeData
2、使用
// 引入 dayjs 和 locale 插件
const dayjs = require('dayjs');
const localeData = require('dayjs/plugin/localeData');
const zh = require('dayjs/locale/zh-cn'); // 需要加載對應(yīng)的語言包
dayjs.extend(localeData);
dayjs.locale(zh);
const date = dayjs('2023-01-01');
console.log(date.format('MMMM D, YYYY')); // 一月 1, 2023自己封裝
原理比較簡單,我們通過解析Date數(shù)據(jù)格式,使用國際化插件配置對應(yīng)的本地化數(shù)據(jù)進(jìn)行格式化填充即可,原理很簡單,但有點(diǎn)費(fèi)時(shí)費(fèi)力,如果實(shí)在無法實(shí)現(xiàn)的時(shí)間格式可以考慮自己封裝實(shí)現(xiàn),具體實(shí)現(xiàn)不提供了。
到此這篇關(guān)于JS實(shí)現(xiàn)Date日期格式的本地化的方法小結(jié)的文章就介紹到這了,更多相關(guān)JS Date格式本地化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS 實(shí)現(xiàn)雙色表格實(shí)現(xiàn)代碼
通過為<tr>元素添加屬性或類型選擇器,再通過CSS設(shè)置可以實(shí)現(xiàn)雙色表格,但如果表格很長,逐個(gè)元素添加可真麻煩。而且這樣的代碼維護(hù)起來不容易。所以比較好的方式是用JS實(shí)現(xiàn)。2009-11-11
跟我學(xué)習(xí)javascript的this關(guān)鍵字
跟我學(xué)習(xí)javascript的this關(guān)鍵字,this是動態(tài)綁定,或稱為運(yùn)行期綁定的,這就導(dǎo)致 JavaScript中的this關(guān)鍵字有能力具備多重含義,帶來靈活性的同時(shí),也為初學(xué)者帶來不少困惑2015-11-11
js實(shí)現(xiàn)對ajax請求面向?qū)ο蟮姆庋b
這篇文章主要介紹了js實(shí)現(xiàn)對ajax請求面向?qū)ο蟮姆庋b的相關(guān)資料,需要的朋友可以參考下2016-01-01
深入理解JavaScript系列(6) 強(qiáng)大的原型和原型鏈
JavaScript 不包含傳統(tǒng)的類繼承模型,而是使用 prototypal 原型模型2012-01-01
javascript實(shí)現(xiàn)3D切換焦點(diǎn)圖
一款用JavaScript模仿3D立體切換效果的js焦點(diǎn)幻燈片特效,使用方法很簡單:用鼠標(biāo)拖拽圖片向左右方向就好~2015-10-10
JavaScript?CSS解析B站的彈幕可以不擋人物原理及技巧
這篇文章主要為大家介紹了JavaScript?CSS解析B站的彈幕可以不擋人物原理及技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
JavaScript樹形菜單功能實(shí)現(xiàn)方法總結(jié)
樹形菜單是前端開發(fā)中常見的交互組件,用于展示具有層級關(guān)系的數(shù)據(jù)(如文件目錄、分類列表、組織架構(gòu)等),這篇文章主要介紹了JavaScript樹形菜單功能實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2025-08-08

