国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁(yè) web前端 js教程 原生粉碎日期和時(shí)間格式:釋放 Intl.DateTimeFormat 的隱藏力量

原生粉碎日期和時(shí)間格式:釋放 Intl.DateTimeFormat 的隱藏力量

Jan 10, 2025 pm 08:28 PM

Crush Date and Time Formatting Natively: Unleash the Hidden Power of Intl.DateTimeFormat

Native Intl API 簡(jiǎn)介

JavaScript 中的 Intl API 是一種強(qiáng)大的本機(jī)解決方案,用于處理各種語(yǔ)言的數(shù)據(jù)(例如日期、數(shù)字和文本)的本地化和格式設(shè)置。與許多第三方庫(kù)不同,這些 API 提供:

  • 高性能:集成到 JavaScript 引擎中。

  • 更小的包大小:消除了對(duì)外部依賴項(xiàng)的需要。

  • 全球支持:包括多種語(yǔ)言和地區(qū)的本地化。

與外部庫(kù)不同,本機(jī) API 不需要開發(fā)人員更新或維護(hù)。此外,它們還針對(duì)底層 JavaScript 引擎進(jìn)行了優(yōu)化,提供了更輕、更快的本地化和格式化方法。

在本文中,我們將重點(diǎn)關(guān)注 Intl.DateTimeFormat,這是一個(gè)專門用于根據(jù)所需本地化設(shè)置日期和時(shí)間格式的 API。我們將了解此 API 如何取代 Moment.js、date-fns 或 Day.js 等流行庫(kù)來(lái)滿足格式化需求,從而提供現(xiàn)代且原生的替代方案。


Intl.DateTimeFormat 的描述

Intl.DateTimeFormat 是一個(gè)允許本地化日期和時(shí)間格式的類。它提供了高級(jí)功能,例如支持不同的本地化、可自定義的格式以及處理替代日歷和時(shí)區(qū)。


創(chuàng)建格式化程序

格式化程序是 Intl.DateTimeFormat 的一個(gè)實(shí)例,它存儲(chǔ)日期格式化的特定配置。通過(guò)使用格式化程序,您可以重復(fù)將相同的格式應(yīng)用于不同的日期,使您的代碼更加高效和可讀。

要?jiǎng)?chuàng)建格式化程序,請(qǐng)使用帶有所需參數(shù)的 Intl.DateTimeFormat 構(gòu)造函數(shù):

const formatter = new Intl.DateTimeFormat(locale, options);
  • locale:定義本地化的字符串(例如,“en-US”表示美式英語(yǔ),“it-IT”表示意大利語(yǔ))。

  • options:用于指定日期組成部分的可選對(duì)象(例如,工作日、月份、年份等)。

雖然將格式化程序?qū)嵗4嬖谧兞恐幸怨┲赜檬呛艹R姷?,但這一步并不是嚴(yán)格要求的。可以直接調(diào)用 Intl.DateTimeFormat 構(gòu)造函數(shù)來(lái)內(nèi)嵌日期格式,如下所示:

console.log(new Intl.DateTimeFormat('en-US', { year: 'numeric', month: 'long', day: 'numeric' }).format(new Date(2024, 11, 19)));
// Output: "December 19, 2024"

但是,當(dāng)需要將相同的格式應(yīng)用于多個(gè)日期時(shí),創(chuàng)建格式化程序?qū)嵗妥兊锰貏e有用,可以提高代碼一致性并避免冗余:

const formatter = new Intl.DateTimeFormat(locale, options);

基本示例:Intl.DateTimeFormat 入門

通過(guò)這些基礎(chǔ)示例探索 Intl.DateTimeFormat 的簡(jiǎn)單性和強(qiáng)大功能。我們將演示如何創(chuàng)建可在您的應(yīng)用程序中重復(fù)使用的默認(rèn)格式和自定義格式的格式化程序。

1. 默認(rèn)格式

如果未提供選項(xiàng),格式化程序?qū)⑹褂盟x語(yǔ)言環(huán)境的默認(rèn)格式。

console.log(new Intl.DateTimeFormat('en-US', { year: 'numeric', month: 'long', day: 'numeric' }).format(new Date(2024, 11, 19)));
// Output: "December 19, 2024"

2. 自定義格式

通過(guò)指定所需的選項(xiàng)來(lái)自定義輸出。

const formatter = new Intl.DateTimeFormat('en-US', {
  year: 'numeric',
  month: 'long',
  day: 'numeric'
});

console.log(formatter.format(new Date(2024, 11, 19))); // Output: "December 19, 2024"
console.log(formatter.format(new Date(2023, 5, 15))); // Output: "June 15, 2023"

高級(jí)示例:釋放 Intl.DateTimeFormat 的全部潛力

通過(guò)高級(jí)方案將日期和時(shí)間格式提升到一個(gè)新的水平,例如處理多個(gè)本地化、備用日歷和時(shí)區(qū)。這些示例展示了 Intl.DateTimeFormat 在復(fù)雜應(yīng)用中的多功能性和適應(yīng)性。

1. 處理不同的本地化

const date = new Date(2024, 11, 19);
const formatter = new Intl.DateTimeFormat('en-US');
console.log(formatter.format(date)); // Output: "12/19/2024"

2. 使用替代日歷進(jìn)行格式化

const date = new Date(2024, 11, 19);
const formatter = new Intl.DateTimeFormat('en-US', {
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric'
});
console.log(formatter.format(date)); // Output: "Thursday, December 19, 2024"

3. 處理時(shí)區(qū)

const date = new Date(2024, 11, 19);
const italianFormatter = new Intl.DateTimeFormat('it-IT', {
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric'
});
console.log(italianFormatter.format(date)); // Output: "giovedì 19 dicembre 2024"

TypeScript 和 Intl.DateTimeFormat

將 Intl.DateTimeFormat 與 TypeScript 結(jié)合使用可確保類型安全和更好的開發(fā)體驗(yàn)。 Intl.DateTimeFormat 的 TypeScript 定義是內(nèi)置的,為其方法和屬性提供自動(dòng)完成和文檔。

這是一個(gè)例子:

const islamicFormatter = new Intl.DateTimeFormat('ar-SA-u-ca-islamic');
console.log(islamicFormatter.format(new Date(2024, 11, 19))); // Output: Date in the Islamic calendar

嚴(yán)格類型化有助于通過(guò)在編譯時(shí)捕獲潛在問(wèn)題(例如不正確的選項(xiàng)或方法調(diào)用)來(lái)避免運(yùn)行時(shí)錯(cuò)誤。


與流行圖書館的比較

為什么考慮 Intl.DateTimeFormat?

  • 輕量級(jí):它是原生的,不需要外部庫(kù),減少了包大小。

  • 性能:通常比基于庫(kù)的解決方案更快。

  • 內(nèi)置本地化:原生支持多種語(yǔ)言和日歷。

示例:簡(jiǎn)單格式設(shè)置

使用 Moment.js

const date = new Date(Date.UTC(2024, 11, 19, 15, 30)); // Ensure the date is set in UTC

// Formatter for UTC
const utcFormatter = new Intl.DateTimeFormat('en-US', {
  timeZone: 'UTC',
  year: 'numeric',
  month: 'short',
  day: '2-digit',
  hour: '2-digit',
  minute: '2-digit',
  timeZoneName: 'short',
});
console.log(utcFormatter.format(date)); 
// Output: "Dec 19, 2024, 03:30 PM UTC"

// Formatter for Tokyo time zone
const tokyoFormatter = new Intl.DateTimeFormat('ja-JP', {
  timeZone: 'Asia/Tokyo',
  year: 'numeric',
  month: 'short',
  day: '2-digit',
  hour: '2-digit',
  minute: '2-digit',
  timeZoneName: 'short',
});
console.log(tokyoFormatter.format(date)); 
// Output: "2024/12/20 00:30 JST"

// Formatter for Berlin time zone
const berlinFormatter = new Intl.DateTimeFormat('de-DE', {
  timeZone: 'Europe/Berlin',
  year: 'numeric',
  month: 'short',
  day: '2-digit',
  hour: '2-digit',
  minute: '2-digit',
  timeZoneName: 'short',
});
console.log(berlinFormatter.format(date)); 
// Output: "19. Dez. 2024, 16:30 MEZ"

帶有 date-fns

const formatter: Intl.DateTimeFormat = new Intl.DateTimeFormat('en-US', {
  year: 'numeric',
  month: 'long',
  day: 'numeric'
});

const formattedDate: string = formatter.format(new Date(2024, 11, 19));
console.log(formattedDate); // Output: "December 19, 2024"

使用 Day.js

const moment = require('moment');
console.log(moment("2024-12-19").format('dddd, MMMM Do YYYY'));

使用 Intl.DateTimeFormat

const { format } = require('date-fns');
console.log(format(new Date(2024, 11, 19), 'EEEE, MMMM do yyyy'));

雖然外部庫(kù)可能提供語(yǔ)法糖,但本機(jī) API 提供了等效的功能,而不會(huì)犧牲靈活性或效率。本機(jī) API 稍長(zhǎng)的語(yǔ)法是對(duì)其在可維護(hù)性、性能和簡(jiǎn)單性方面的優(yōu)勢(shì)的一個(gè)小小的權(quán)衡。


結(jié)論

Intl.DateTimeFormat 為日期和時(shí)間格式化提供了強(qiáng)大的本機(jī)解決方案,使其成為 Moment.js、date-fns 和 Day.js 等流行庫(kù)的絕佳替代品。憑借其高性能、內(nèi)置本地化和簡(jiǎn)化的維護(hù),它是現(xiàn)代 JavaScript 應(yīng)用程序的寶貴工具。

要深入了解 Intl.DateTimeFormat 并探索其他功能,請(qǐng)?jiān)L問(wèn)官方 MDN Web 文檔。在那里,您將找到全面的文檔和實(shí)際示例,幫助您掌握這個(gè)強(qiáng)大的 API。

以上是原生粉碎日期和時(shí)間格式:釋放 Intl.DateTimeFormat 的隱藏力量的詳細(xì)內(nèi)容。更多信息請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機(jī)

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁(yè)開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)代碼編輯軟件(SublimeText3)

Java vs. JavaScript:清除混亂 Java vs. JavaScript:清除混亂 Jun 20, 2025 am 12:27 AM

Java和JavaScript是不同的編程語(yǔ)言,各自適用于不同的應(yīng)用場(chǎng)景。Java用于大型企業(yè)和移動(dòng)應(yīng)用開發(fā),而JavaScript主要用于網(wǎng)頁(yè)開發(fā)。

JavaScript評(píng)論:簡(jiǎn)短說(shuō)明 JavaScript評(píng)論:簡(jiǎn)短說(shuō)明 Jun 19, 2025 am 12:40 AM

JavascriptconcommentsenceenceEncorenceEnterential gransimenting,reading and guidingCodeeXecution.1)單inecommentsareusedforquickexplanations.2)多l(xiāng)inecommentsexplaincomplexlogicorprovideDocumentation.3)

如何在JS中與日期和時(shí)間合作? 如何在JS中與日期和時(shí)間合作? Jul 01, 2025 am 01:27 AM

JavaScript中的日期和時(shí)間處理需注意以下幾點(diǎn):1.創(chuàng)建Date對(duì)象有多種方式,推薦使用ISO格式字符串以保證兼容性;2.獲取和設(shè)置時(shí)間信息可用get和set方法,注意月份從0開始;3.手動(dòng)格式化日期需拼接字符串,也可使用第三方庫(kù);4.處理時(shí)區(qū)問(wèn)題建議使用支持時(shí)區(qū)的庫(kù),如Luxon。掌握這些要點(diǎn)能有效避免常見錯(cuò)誤。

為什么要將標(biāo)簽放在的底部? 為什么要將標(biāo)簽放在的底部? Jul 02, 2025 am 01:22 AM

PlacingtagsatthebottomofablogpostorwebpageservespracticalpurposesforSEO,userexperience,anddesign.1.IthelpswithSEObyallowingsearchenginestoaccesskeyword-relevanttagswithoutclutteringthemaincontent.2.Itimprovesuserexperiencebykeepingthefocusonthearticl

JavaScript與Java:開發(fā)人員的全面比較 JavaScript與Java:開發(fā)人員的全面比較 Jun 20, 2025 am 12:21 AM

JavaScriptIspreferredforredforwebdevelverment,而Javaisbetterforlarge-ScalebackendsystystemsandSandAndRoidApps.1)JavascriptexcelcelsincreatingInteractiveWebexperienceswebexperienceswithitswithitsdynamicnnamicnnamicnnamicnnamicnemicnemicnemicnemicnemicnemicnemicnemicnddommanipulation.2)

JavaScript:探索用于高效編碼的數(shù)據(jù)類型 JavaScript:探索用于高效編碼的數(shù)據(jù)類型 Jun 20, 2025 am 12:46 AM

javascripthassevenfundaMentalDatatypes:數(shù)字,弦,布爾值,未定義,null,object和symbol.1)numberSeadUble-eaduble-ecisionFormat,forwidevaluerangesbutbecautious.2)

什么是在DOM中冒泡和捕獲的事件? 什么是在DOM中冒泡和捕獲的事件? Jul 02, 2025 am 01:19 AM

事件捕獲和冒泡是DOM中事件傳播的兩個(gè)階段,捕獲是從頂層向下到目標(biāo)元素,冒泡是從目標(biāo)元素向上傳播到頂層。1.事件捕獲通過(guò)addEventListener的useCapture參數(shù)設(shè)為true實(shí)現(xiàn);2.事件冒泡是默認(rèn)行為,useCapture設(shè)為false或省略;3.可使用event.stopPropagation()阻止事件傳播;4.冒泡支持事件委托,提高動(dòng)態(tài)內(nèi)容處理效率;5.捕獲可用于提前攔截事件,如日志記錄或錯(cuò)誤處理。了解這兩個(gè)階段有助于精確控制JavaScript響應(yīng)用戶操作的時(shí)機(jī)和方式。

Java和JavaScript有什么區(qū)別? Java和JavaScript有什么區(qū)別? Jun 17, 2025 am 09:17 AM

Java和JavaScript是不同的編程語(yǔ)言。1.Java是靜態(tài)類型、編譯型語(yǔ)言,適用于企業(yè)應(yīng)用和大型系統(tǒng)。2.JavaScript是動(dòng)態(tài)類型、解釋型語(yǔ)言,主要用于網(wǎng)頁(yè)交互和前端開發(fā)。

See all articles