将日期时间字符串转换为 utc
我有一个输入时间设置,如下所示。
{
"effectiveFrom": "2022-03-24 06:28",
}
我需要将其转换为 UTC 格式,就像这样
{
"effective_from": "2022-03-24T00:58:00.000Z",
}
我该怎么做,我在这里使用此代码,但这没有按预期工作任何帮助都很棒。
新日期(ctx.params.dateTime);
I have an input time setting which is like this.
{
"effectiveFrom": "2022-03-24 06:28",
}
I need to convert the same to UTC format which is like this
{
"effective_from": "2022-03-24T00:58:00.000Z",
}
How can i do that I am using this code over here but this is not working as expected any help is great.
new Date(ctx.params.dateTime);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来源:MDN 网络文档
.toISOString()
将为您提供您所请求的格式。但是您无法对其执行任何日期操作,因为您得到的输出是一个字符串。我刚刚创建了一个简单的节点文件并检查你哪里做错了。
顺便说一句,我使用了sequelize和postgres进行了测试。
Source: MDN Web Docs
.toISOString()
will give you the format you are requesting. But you can't perform any date operations on that since the output you get is a string.I just created a simple node file and check where you are doing wrong.
By the way, I have used sequelize and postgres for testing.
当您使用
new Date(ctx.params.dateTime)
解析日期字符串时,您将得到一个 返回日期对象。要获取 UTC 字符串作为输出,您可以调用 该对象上的
toISOString()
方法。When you parse the date string using
new Date(ctx.params.dateTime)
you will get a Date object in return.To get the UTC string as output, you can call the
toISOString()
method on that object.