如何格式化日期以始终显示为 UTC
How to format a date to always show as UTC
我正在尝试格式化日期,使其始终显示为 UTC,例如格式为 'dd/mm/YYYY HH:MM UTC'。到目前为止我有这个代码:
const options = {
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
timezone: 'UTC',
timeZoneName: 'short'
};
const formattedTime = new Intl.DateTimeFormat('en-GB', options).format(new Date(unixTimestamp))
但是这给了我 GMT 或 BST 时间,例如
24/11/2021, 05:51 GMT
27/10/2021, 05:09 BST
我希望时间始终显示为 UTC,因此在上述示例中:
24/11/2021, 05:51 UTC
27/10/2021, 04:09 UTC
有没有一种方法可以配置 DateTimeFormat 来执行此操作,还是我应该使用其他方法?如果可能的话,我宁愿不求助于导入库。
格式化选项对象的 timezone
属性 必须采用驼峰式 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#timezone
我正在尝试格式化日期,使其始终显示为 UTC,例如格式为 'dd/mm/YYYY HH:MM UTC'。到目前为止我有这个代码:
const options = {
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
timezone: 'UTC',
timeZoneName: 'short'
};
const formattedTime = new Intl.DateTimeFormat('en-GB', options).format(new Date(unixTimestamp))
但是这给了我 GMT 或 BST 时间,例如
24/11/2021, 05:51 GMT
27/10/2021, 05:09 BST
我希望时间始终显示为 UTC,因此在上述示例中:
24/11/2021, 05:51 UTC
27/10/2021, 04:09 UTC
有没有一种方法可以配置 DateTimeFormat 来执行此操作,还是我应该使用其他方法?如果可能的话,我宁愿不求助于导入库。
格式化选项对象的 timezone
属性 必须采用驼峰式 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#timezone