js中對new Date() 中轉換字串方法toLocaleString的使用。
提供特定於區域設定的日期和時間格式。
dateTimeFormatObj = new Intl.DateTimeFormat([locales][, options])
dateTimeFormatObj
必需。將 DateTimeFormat 物件分配到的變數名。
locales
可選。包含一種或多種語言或區域設定標記的區域設定字串陣列。如果包含多個區域設定字串,請以降序優先順序對它們進行排列,確保首個條目為首選區域設定。如果省略此引數,則使用 JavaScript 執行時的預設區域設定。有關更多資訊,請參見備註部分。
options
可選。包含指定日期和時間格式設定選項的一個或多個特性的物件。有關詳細資訊,請參見“備註”部分。
locales 引數必須符合 BCP 47 語言或“en-us”和“zh-CN”等區域設定標記。標記可包括語言、區域、國家/地區和變數。有關語言標記的示例,請參見 BCP 47 的附錄 A。對於 DateTimeFormat,你可能需在區域設定字串中新增一個 -u 子標記以包含一個或兩個以下 Unicode 擴充套件:
-
-nu 指定編號系統擴充套件:language-region-u-nu-numberingsystem
其中 numberingsystem 可為以下各項之一:阿拉伯數字、阿拉伯文數字、巴釐數字、孟加拉數字、梵文數字、全形數字、古吉拉特數字、果魯穆奇數字、漢語數字、高棉數字、坎納達數字、寮國數字、拉丁數字、林布數字、馬拉雅拉姆數字、蒙古數字、緬甸數字、歐迪亞數字、泰米爾數字、泰盧固數字、泰語數字、藏語數字。
-
–ca 指定日曆:language-region-u-ca-calendar
其中 calendar 可為以下各項之一:佛曆、農曆、公曆、回曆及和歷。
options 引數可包括以下屬性:
屬性 |
說明 |
可能的值: |
預設值 |
---|---|---|---|
localeMatcher |
指定要使用的區域設定匹配演算法。 |
"lookup"、"best fit" |
"best fit" |
formatMatcher | 指定要使用的格式匹配演算法。 |
"basic"、"best fit" |
"best fit" |
hour12 |
指定是否對小時使用 12 小時格式。 |
true(12 小時格式)、false(24 小時格式) |
|
timeZone |
指定時區。至少 "UTC" 始終受支援。 |
一個時區值,如 "UTC"。 |
"UTC" |
weekday |
指定週日期的格式設定。 |
"narrow"、"short"、"long"。 |
undefined |
era |
指定紀元的格式設定。 |
"narrow"、"short"、"long" |
undefined |
year |
指定年份的格式設定。 |
"2-digit"、"numeric" |
undefined 或 "numeric" |
month |
指定月份的格式設定。 |
"2-digit"、"numeric"、"narrow"、"short"、"long" |
undefined 或 "numeric" |
day |
指定日的格式設定。 |
"2-digit"、"numeric" |
undefined 或 "numeric" |
hour |
指定小時的格式設定。 |
"2-digit"、"numeric" |
undefined |
minute |
指定分鐘的格式設定。 |
"2-digit"、"numeric" |
undefined |
second |
指定秒的格式設定。 |
"2-digit"、"numeric" |
undefined |
timeZoneName |
指定時區的格式設定。目前不支援此屬性。 |
"short"、"long"。 |
目前不支援此屬性。 |
weekday、era、year、month、day、hour、minute 和 second 的預設值為 undefined。如果不設定這些屬性,則 year、month 和 day 使用 "numeric" 格式。
每個區域設定必須至少支援以下格式:
-
週日期、年、月、日、小時、分鐘、秒
-
週日期、年、月、日
-
年、月、日
-
年、月
-
月、日
-
小時、分鐘、秒
-
小時、分鐘
下表列出了 DateTimeFormat 物件的屬性。
屬性 |
說明 |
指定建立日期/時間格式化程式物件的函式。 |
|
返回利用日期/時間格式化程式設定對特定於區域設定的日期設定格式的函式。 |
|
返回對日期/時間格式化程式原型的引用。 |
下表列出了 DateTimeFormat 物件的方法。
方法 |
說明 |
返回包含日期/時間格式化程式物件的屬性和值的物件。 |
以下示例演示使用不同的區域設定將日期物件傳遞給 DateTimeFormat 的結果。
var date = new Date(Date.UTC(2013, 1, 1, 14, 0, 0)); var options = { weekday: "long", year: "numeric", month: "short", day: "numeric" }; if (console && console.log) { console.log(new Intl.DateTimeFormat("en-US").format(date)); // Returns 2/1/2013 console.log(new Intl.DateTimeFormat("ja-JP").format(date)); // Returns 2013年2月1日 console.log(new Intl.DateTimeFormat("ar-SA", options).format(date)); // Returns الجمعة, ٢٠ ربيع الأول, ١٤٣٤ console.log(new Intl.DateTimeFormat("hi-IN", options).format(date)); // Returns शुक्रवार, 01 फरवरी 2013 }
以下示例將建立一個 DateTimeFormat 物件,它會指定當前週日期採用長格式並使用阿拉伯語(沙烏地阿拉伯)區域設定、回曆和拉丁語數字系統。
var dtf = new Intl.DateTimeFormat(["ar-SA-u-ca-islamic-nu-latn"], { weekday: "long", year: "numeric", day: "numeric", month: "long" }); If (console && console.log) { console.log(dtf.format(new Date())); // Returns الجمعة, 19 رمضان, 1434 }
在 Internet Explorer 11 標準文件模式下支援此項。此外,也在應用商店應用(Windows 8.1 和 Windows Phone 8.1)中受支援。請參閱版本資訊。
在以下文件模式中不受支援:Quirks、Internet Explorer 6 標準模式、Internet Explorer 7 標準模式、Internet Explorer 8 標準模式、Internet Explorer 9 標準模式和 Internet Explorer 10 標準模式。在 Windows 8 中不受支援。
收藏於 2018-08-22
https://msdn.microsoft.com/zh-cn/library/dn342822(v=vs.94).aspx