看起来当我使用该format()函数时,它会自动将原始 UTC 时间转换为我的时区 (UTC+8)。我一直在挖掘他们的文档数小时,似乎无法找到将其默认为 UTC 时间的方法。
import { parseISO, format } from "date-fns";
const time = "2019-10-25T08:10:00Z";
const parsedTime = parseISO(time);
console.log(parsedTime); // 2019-10-25T08:10:00.000Z
const formattedTime = format(parsedTime, "yyyy-MM-dd kk:mm:ss");
console.log(formattedTime); // 2019-10-25 16:10:00 <-- 8 HOURS OFF!!
Run Code Online (Sandbox Code Playgroud)
我试过使用这个包data-fns-tz并使用类似的东西
format(parsedTime, "yyyy-MM-dd kk:mm:ss", {timeZone: "UTC"});
Run Code Online (Sandbox Code Playgroud)
仍然没有运气。
请帮忙!
2019-10-25 08:10:00
2019-10-25 16:10:00