在 JavaScript 中将日期时间字符串转换为 UTC
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31163409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert datetime string to UTC in JavaScript
提问by iamsumesh
How can we convert datetime string to UTC in javascript. We are getting following JSON from REST service
我们如何在 javascript 中将日期时间字符串转换为 UTC。我们正在从 REST 服务获取以下 JSON
[
{
"CreationTime":"June 2, 2015 8:04:53 PM IST",
"category":"UI",
"severity":"MAJOR",
"source":"BILLING",
"status":"ASSIGNED"
}
]
we are able to get CreationTime
into a String variable but unable to convert to UTC. Any idea to convert this?
我们能够CreationTime
进入一个字符串变量,但无法转换为 UTC。有什么想法可以转换这个吗?
回答by Walter Chapilliquen - wZVanG
Using toUTCString():
var toUTC = new Date("June 2, 2015 8:04:53").toUTCString()
In Javascript you can use this method to convert a date from a Date() object
, but not from a IST string
. So you need format this string to a Date() object
, then you can convert it to UTC. In thistopic says what I mean.
在 Javascript 中,您可以使用此方法将日期从 a 转换Date() object
,但不能从IST string
. 因此,您需要将此字符串格式化为 a Date() object
,然后您可以将其转换为 UTC。在这个主题中说出我的意思。
NoteIf you try June 2, 2015 8:04:53 PM IST
JavasScript take it as invalid date, for that you have to use .replace()
function to remove the IST
part of the string.
Note如果您尝试使用June 2, 2015 8:04:53 PM IST
JavaScript 将其视为无效日期,则您必须使用.replace()
函数来删除IST
字符串的一部分。