繁体   English   中英

如何在本机反应中修复 moment.js 不准确的日期格式和时间?

[英]How to fix inaccurate date format and time of moment.js in react native?

我担心日期格式和时间,现在我有2022-09-12T21:18:38.000Z的记录,然后在格式化 created_at 之后结果是 2022 年 9 月 13 日 05:18:38。 问题是格式化后的 9 月 12 日的真实数据变成了 9 月 13 日。

Package 瞬间版本:

"moment": "^2.29.4",
"moment-timezone": "^0.5.37",

示例代码:

import moment from "moment";
import 'moment-timezone';

<Text fontSize="12" mt="2" fontWeight="300">{moment.tz(res?.created_at, "Asia/Manila").format("MMMM DD YYYY HH:mm:ss")}</Text>
<Text>{res?.created_at}</Text>

当前 Output:

电流输出

您有一个以 Z 结尾的时间字符串,即 Zulu 或 UTC+0。 你正在使用.moment.tz 和“亚洲/马尼拉”。 “亚洲/马尼拉”是UTC+8。 UTC+0 的 2022-09-12T21:18:38.000Z 是 UTC+8 的 2022 年 9 月 13 日 05:18:38。

如果您想将此日期格式化为 UTC 时间字符串 - 您可以使用以下任何一种:

使用时刻时区:

moment.tz("2022-09-12T21:18:38.000Z", "UTC").format("MMMM DD YYYY HH:mm:ss")
=> 'September 12 2022 21:18:38'

使用简单的时刻

moment.utc("2022-09-12T21:18:38.000Z").format("MMMM DD YYYY HH:mm:ss")
=> 'September 12 2022 21:18:38'

所以它会适合你:

{moment.tz(res?.created_at, "UTC").format("MMMM DD YYYY HH:mm:ss")}

or

{moment.utc(res?.created_at).format("MMMM DD YYYY HH:mm:ss")}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM