[英]How to fix inaccurate date format and time of moment.js in react native?
I have concern regarding date formatting and the time, happens right now I have record of 2022-09-12T21:18:38.000Z then after formatting that created_at the result is September 13 2022 05:18:38.我担心日期格式和时间,现在我有2022-09-12T21:18:38.000Z的记录,然后在格式化 created_at 之后结果是 2022 年 9 月 13 日 05:18:38。 The problem is the real data September 12 in formatted becomes September 13.问题是格式化后的 9 月 12 日的真实数据变成了 9 月 13 日。
Package Moment Version: Package 瞬间版本:
"moment": "^2.29.4",
"moment-timezone": "^0.5.37",
Sample Code:示例代码:
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>
Current Output:当前 Output:
You have a time string with Z at the end, which is Zulu, or UTC+0.您有一个以 Z 结尾的时间字符串,即 Zulu 或 UTC+0。 And you are using.moment.tz with "Asia/Manila".你正在使用.moment.tz 和“亚洲/马尼拉”。 And "Asia/Manila" is UTC+8. “亚洲/马尼拉”是UTC+8。 And 2022-09-12T21:18:38.000Z in UTC+0 is September 13 2022 05:18:38 for UTC+8. UTC+0 的 2022-09-12T21:18:38.000Z 是 UTC+8 的 2022 年 9 月 13 日 05:18:38。
If you want to format this date to UTC time string - you can use any of those:如果您想将此日期格式化为 UTC 时间字符串 - 您可以使用以下任何一种:
Using moment timezone:使用时刻时区:
moment.tz("2022-09-12T21:18:38.000Z", "UTC").format("MMMM DD YYYY HH:mm:ss")
=> 'September 12 2022 21:18:38'
Using plain moment使用简单的时刻
moment.utc("2022-09-12T21:18:38.000Z").format("MMMM DD YYYY HH:mm:ss")
=> 'September 12 2022 21:18:38'
So it will be for you:所以它会适合你:
{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.