javascript - 如何使用 'Temporal' API在不同的日历日期之间进行转换

标签 javascript date datetime calendar ecmascript-temporal

使用 Temporal.Calendar Temporal 即将提出的建议global 对象,以下短函数在日历日期(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/polyfill@0.3.0/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> 

最佳答案

您可以将 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 });
}

关于javascript - 如何使用 'Temporal' API在不同的日历日期之间进行转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71265710/

相关文章:

javascript - 在列中打印动态数据

ios - NSDateFormatter 日期格式类型

Java时区: how to get UTC time values based on Console settings?

mysql - 如何从日期时间列中获取两小时之间的行?

jquery - 使用 jquery 和 Json 时日期值显示格式错误

javascript - 为什么我不能将此 Prop 传递给子组件?

javascript - 慢慢地动画一条简单的线

javascript - 使用 Widget 代码中的 javascript 将光标图像左滑向右移动

java - 如何将时间从 GMT 转换为 EST

javascript - 处理阿拉伯语言区域时正确格式化本地 UTC 日期字符串