我有一个日志文件,其中包含一些时间戳
2020-12-03 08:30:00
2020-12-03 08:40:00
...
我从日志提供程序的文档中得知时间戳是以协调世界时(UTC)编写的(虽然没有使用ISO格式)
现在我想要使用date-fns来解析它们:
const toParse = "2020-12-03 08:40:00"
parse(toParse, 'yyyy-MM-dd HH:mm:ss', new Date()).toISOString()
因为我的电脑时区是UTC+1,所以这是我看到的:
> "2020-12-03T07:40:00Z"
预期结果:
> "2020-12-03T08:40:00Z".
以下是我目前使用的技巧,用于告诉date-fns将时间解析为UTC:
const toParse = "2020-12-03 08:40:00"
parse(toParse + '+00', 'yyyy-MM-dd HH:mm:ss' + 'X', new Date()).toISOString()
正如预期的那样,
> "2020-12-03T08:40:00Z".
有没有使用date-fns的适当方法来做到这一点?寻找一个与moment的moment.utc()
相等的方法。
parseIso(toParse+'Z').toISOString()
。 - Anatoly