最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS實(shí)現(xiàn)Date日期格式的本地化的方法小結(jié)

 更新時(shí)間:2024年06月21日 09:26:23   作者:胖蔡  
為了更好的更新多語言日期的顯示,所以希望實(shí)現(xiàn)日期的本地化格式顯示要求,常規(guī)的特殊字符型格式化無法滿足顯示要求,這里整理了幾種我思考實(shí)現(xiàn)的本地化實(shí)現(xiàn)功能

引言

為了更好的更新多語言日期的顯示,所以希望實(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)代碼

    JS 實(shí)現(xiàn)雙色表格實(shí)現(xiàn)代碼

    通過為<tr>元素添加屬性或類型選擇器,再通過CSS設(shè)置可以實(shí)現(xiàn)雙色表格,但如果表格很長,逐個(gè)元素添加可真麻煩。而且這樣的代碼維護(hù)起來不容易。所以比較好的方式是用JS實(shí)現(xiàn)。
    2009-11-11
  • JS模擬Dialog彈出浮動(dòng)框效果代碼

    JS模擬Dialog彈出浮動(dòng)框效果代碼

    這篇文章主要介紹了JS模擬Dialog彈出浮動(dòng)框效果代碼,涉及JavaScript可拖動(dòng)窗口的創(chuàng)建及布局相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • 跟我學(xué)習(xí)javascript的this關(guān)鍵字

    跟我學(xué)習(xí)javascript的this關(guān)鍵字

    跟我學(xué)習(xí)javascript的this關(guān)鍵字,this是動(dòng)態(tài)綁定,或稱為運(yùn)行期綁定的,這就導(dǎo)致 JavaScript中的this關(guān)鍵字有能力具備多重含義,帶來靈活性的同時(shí),也為初學(xué)者帶來不少困惑
    2015-11-11
  • 原生JS實(shí)現(xiàn)特效留言框

    原生JS實(shí)現(xiàn)特效留言框

    這篇文章主要為大家詳細(xì)介紹了原生JS實(shí)現(xiàn)特效留言框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • js實(shí)現(xiàn)對ajax請求面向?qū)ο蟮姆庋b

    js實(shí)現(xiàn)對ajax請求面向?qū)ο蟮姆庋b

    這篇文章主要介紹了js實(shí)現(xiàn)對ajax請求面向?qū)ο蟮姆庋b的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • 深入理解JavaScript系列(6) 強(qiáng)大的原型和原型鏈

    深入理解JavaScript系列(6) 強(qiáng)大的原型和原型鏈

    JavaScript 不包含傳統(tǒng)的類繼承模型,而是使用 prototypal 原型模型
    2012-01-01
  • javascript實(shí)現(xiàn)3D切換焦點(diǎn)圖

    javascript實(shí)現(xiàn)3D切換焦點(diǎn)圖

    一款用JavaScript模仿3D立體切換效果的js焦點(diǎn)幻燈片特效,使用方法很簡單:用鼠標(biāo)拖拽圖片向左右方向就好~
    2015-10-10
  • 解決layui的table.checkStatus失效問題

    解決layui的table.checkStatus失效問題

    這篇文章主要介紹了解決layui的table.checkStatus失效問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • JavaScript?CSS解析B站的彈幕可以不擋人物原理及技巧

    JavaScript?CSS解析B站的彈幕可以不擋人物原理及技巧

    這篇文章主要為大家介紹了JavaScript?CSS解析B站的彈幕可以不擋人物原理及技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • JavaScript樹形菜單功能實(shí)現(xiàn)方法總結(jié)

    JavaScript樹形菜單功能實(shí)現(xiàn)方法總結(jié)

    樹形菜單是前端開發(fā)中常見的交互組件,用于展示具有層級關(guān)系的數(shù)據(jù)(如文件目錄、分類列表、組織架構(gòu)等),這篇文章主要介紹了JavaScript樹形菜單功能實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下
    2025-08-08

最新評論

阿克苏市| 泸溪县| 哈密市| 比如县| 浦东新区| 谢通门县| 南涧| 临海市| 金寨县| 纳雍县| 肥乡县| 甘泉县| 克拉玛依市| 武夷山市| 弥渡县| 旬阳县| 京山县| 都兰县| 伊宁县| 塘沽区| 漾濞| 开平市| 林西县| 嘉峪关市| 富宁县| 始兴县| 石柱| 南溪县| 滦南县| 曲水县| 文水县| 钟祥市| 吉木萨尔县| 奎屯市| 定远县| 华阴市| 宜宾县| 长治县| 秭归县| 育儿| 登封市|