泰国文化日期转换两次


export const lastUpdated = createSelector(
learningDetails,
ld =>
{
(ld.lastModifiedDate) &&
Intl.DateTimeFormat('th-TH', {
month: '2-digit',
day: '2-digit',
year: 'numeric',
}).format(new Date(ld.lastModifiedDate))
} 
);//returns >> 3107-05-21

上面是一个函数,它提取最后一个更新日期,因为通过格式化日期值,从服务接收到的ld.lastModifiedDate已经在泰国时区,所以它会再次被转换。例如:

原始日期:2021-05-21 UTC在服务层转换为泰国时区2564-05-21,即543年后(因为泰国文化遵循佛教日历(。服务退货&将ld.lastModifiedDate填充到2564-05-21,然后根据区域性使用Intl.DateTimeFormat进行格式化,这导致年份再次增加543年。(2021+543+543(,这是不正确的。有没有一种动态的方法可以避免所有文化在格式化时的第二次转换。

注意:我们不能更改遗留服务层以返回UTC/原始日期,因为其他内部函数正在调用它。

从以下位置获取解决方案:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat

基本上,通过在文化中添加"-u-ca-gregory"来解决问题。默认为公历。

export const lastUpdated = createSelector(
learningDetails,
ld =>
{
(ld.lastModifiedDate) &&
Intl.DateTimeFormat('th-TH-u-ca-gregory', {
month: '2-digit',
day: '2-digit',
year: 'numeric',
}).format(new Date(ld.lastModifiedDate))
} 
);//returns >> 2564-05-21

最新更新