date-fns |如何格式化为UTC [英] date-fns | How do I format to UTC
本文介绍了date-fns |如何格式化为UTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用 format()
函数时,它会自动转换原始UTC时间进入我的时区(UTC + 8)。我一直在研究他们的文档几个小时,似乎找不到默认的UTC时间。
It looks like when I use the format()
function, it automatically convert the original UTC time into my timezone (UTC+8). I have been digging through their docs for hours and couldn't seem to find a way to default it to UTC time.
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!!
我尝试使用软件包 data-fns-tz
,并使用类似的
I have tried to use the package data-fns-tz
and use something like
format(parsedTime, "yyyy-MM-dd kk:mm:ss", {timeZone: "UTC"});
仍然没有运气。
请帮助!
2019-10-25 08:10:00
2019-10-25 16:10:00
推荐答案
我建议使用内置的 Date
实用程序:
I would suggest using the built-in Date
util:
const date = new Date("2019-10-25T08:10:00Z");
const isoDate = date.toISOString();
console.log(`${isoDate.substr(0, 10)} ${isoDate.substr(11, 8)}`);
输出:
2019-10-25 08:10:00
不是任何格式的通用解决方案,但不需要外部库。
Not a general solution for any format, but no external libraries required.
这篇关于date-fns |如何格式化为UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文