使用即將提出的Temporal全域物件的Temporal.Calendar,以下短函式在日歷日期(Javascript 中識別的 18 個日歷)之間進行轉換。
目前,由 Temporal.Calendar 回傳的其他日歷的輸出日期格式為(示例):'2022-02-25[u-ca=persian]'
如何避免使用toString().split("[")[0])獲取沒有后綴的日歷日期[u-ca=CalendarName]作為Intl.DateTimeFormat()不識別后綴?
<script type='module'>
// ====== load Temporray polyfill (not needed after full Temporal implementation) ========
import * as TemporalModule from 'https://cdn.jsdelivr.net/npm/@js-temporal/[email protected]/dist/index.umd.js'
//=====================================================================
// Y,M,D are the date to convert from
// options as in Intl.DateTimeFormat() with additional 'from' and 'locale'
// 'from': calendar name to convert from
// 'locale' the locale to use (default 'en')
// 'calendar': calendar to convert to
// All other options as in Intl.DateTimeFormat()
//---------------------------------------
function dateToCalendars(Y, M, D, op={}) {
return new Intl.DateTimeFormat(op.locale??="en",op).format(new Date(temporal.Temporal.PlainDateTime.from({year:Y,month:M,day:D,calendar:op.from??= "gregory"}).toString().split("[")[0]));
}
//---------------------------------------
//===============================
// Example Test Cases
//===============================
console.log(dateToCalendars(2022,2,25)); // default Gregory
// full format Gregory
console.log(dateToCalendars(2022,2,25, {dateStyle: "full"}));
// from Persian to Gregory in full format
console.log(dateToCalendars(1400,12,6,{from: 'persian', dateStyle: "full"}));
// from Persian to Gregory in full format in 'fa' locale
console.log(dateToCalendars(1400,12,6,{from: 'persian', dateStyle: "full", locale: "fa"}));
// from Persian to Islamic in full format in 'fa' locale
console.log(dateToCalendars(1400,12,6,{from: 'persian', calendar: 'islamic', dateStyle:"full", locale: "fa"}));
// from Islamic to Gregory full format (default 'en' locale)
console.log(dateToCalendars(1443,7,24,{from:"islamic", dateStyle:"full"}));
// from Hebrew to Islamic in full format
console.log(dateToCalendars(5782,6,24,{from: 'hebrew', calendar:"islamic", dateStyle:"full"}));
// from Hebrew to Islamic in full format in 'ar' locale
console.log(dateToCalendars(5782,6,24,{from: 'hebrew', calendar:"islamic", dateStyle:"full", locale: 'ar'}));
// from Hebrew to Persian in full format
console.log(dateToCalendars(5782,6,24,{from: 'hebrew', calendar:"persian", dateStyle:"full"}));
// from Hebrew to Gregory in full format in 'he' locale
console.log(dateToCalendars(5782,6,24,{from: 'hebrew', dateStyle:"full", locale: 'he'}));
</script>
uj5u.com熱心網友回復:
您可以將 Temporal.PlainDateTime 物件直接傳遞給 Intl.DateTimeFormat,或使用 Temporal.Calendar.prototype.toLocaleString() 間接傳遞。這應該使您不必拆分字串以洗掉括號。
(一個好的經驗法則是,如果您發現自己使用任何 Temporal 物件的 toString() 的輸出進行字串操作,或者為此使用new Date(),這可能表明您應該使用 Temporal 方法。)
需要注意的是,您必須確保語言環境的日歷與您正在格式化的日期的日歷相匹配。您不能使用 toLocaleString() 或 Intl.DateTimeFormat 進行日歷轉換(除非它來自 ISO 8601 日歷)。因此,您應該使用 withCalendar() 方法將日期轉換為要輸出的日歷,并確保 Intl 選項中的日歷與之匹配。
這是我對這樣一個功能的嘗試:
function dateToCalendars(year, month, day, op = {}) {
const fromCalendar = op.from ?? 'gregory';
const toCalendar = op.calendar ?? fromCalendar;
const date = Temporal.PlainDate.from({ year, month, day, calendar: fromCalendar })
.withCalendar(toCalendar);
return date.toLocaleString(op.locale ?? 'en', { ...op, calendar: toCalendar });
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/434266.html
標籤:javascript 日期 约会时间 日历
上一篇:如何在使用chrono庫將數字轉換為日期的cpp中的main()中正確呼叫此模板函式?
下一篇:如何在火花中過濾可變日期?
