My service is returning this as date 7/14/2016 2:40 AM +00:00
. How can I convert this to UTC in JS?
我的服务是在日期7/14/2016 2:40 AM +00:00返回。如何在JS中将其转换为UTC?
Tried:
new Date("7/14/2016 2:40 AM +00:00").toISOString();
In database the date has been stored as UTC so when I will display the date I want to display as Local time.
在数据库中,日期已存储为UTC,因此当我将显示我想要显示为当地时间的日期时。
3 个解决方案
#1
1
There are many ways to parse a string to produce a Date
object.
有许多方法可以解析字符串以生成Date对象。
-
One way is with the
Date
object itself, either by passing a string to the constructor, or by usingDate.parse
. However, only the ISO8601 formats are required in the ECMAScript specification. Any other input is implementation specific, and may or may not be recognized by the different JavaScript runtimes. With web browsers in particular, there are many differences in supported formats across browsers.一种方法是使用Date对象本身,或者通过将字符串传递给构造函数,或者使用Date.parse。但是,ECMAScript规范中只需要ISO8601格式。任何其他输入都是特定于实现的,并且可能会或可能不会被不同的JavaScript运行时识别。特别是在Web浏览器中,跨浏览器支持的格式存在许多差异。
Additionally, the locale of the environment plays a factor in how the values are parsed. How should
1/2/2016
be parsed? January 2nd, or February 1st? You showed an example of7/14/2016
, which would be invalid input if ran in the UK (for example), where the date format isDD/MM/YYYY
.此外,环境的区域设置是解析值的方式的一个因素。应该如何解析1/2/2016? 1月2日还是2月1日?您展示了7/14/2016的示例,如果在英国(例如)运行,则输入无效,其中日期格式为DD / MM / YYYY。
-
You can write custom code to split the string up into its parts, parse each part individually, and compose a result. The main problem with this approach is that the code tends to be rigid, and sometimes fragile. It should be well tested, and many edge cases need to be considered.
您可以编写自定义代码以将字符串拆分为其各个部分,单独解析每个部分并撰写结果。这种方法的主要问题是代码往往是僵化的,有时是脆弱的。它应该经过充分测试,并且需要考虑许多边缘情况。
-
You can use a library, which is by far the easiest and most flexible approach (IMHO). With a good library, you can take comfort in the shared experiences of others, and in the unit tests that are (hopefully) part of the library you choose. Of course, using a library comes with several tradeoffs including increased file size, and relinquishing some degree of control. You should evaluate these tradeoffs carefully.
您可以使用库,这是迄今为止最简单,最灵活的方法(恕我直言)。有了一个好的图书馆,您可以在其他人的共享体验中获得安慰,并且可以在(希望)您选择的图书馆中进行单元测试。当然,使用库有几个权衡,包括增加文件大小,并放弃一定程度的控制。您应该仔细评估这些权衡。
There are many date libraries available for JavaScript. The most popular is probably moment.js, though there are others to choose from, and some larger frameworks sometimes have similar functionality already included.
有许多可用于JavaScript的日期库。最受欢迎的可能是moment.js,尽管还有其他人可供选择,而一些较大的框架有时会包含类似的功能。
Here is an example using moment.js:
以下是使用moment.js的示例:
var i = "7/14/2016 2:40 AM +00:00"; var f = "M/D/YYYY h:mm A Z"; var m = moment(i, f); var o = m.format(f); // will be in the local time, in the same format as the input var d = m.toDate(); // if you really want a Date object
#2
0
Assuming you can guarantee that format for all dates, the following code will suffice:
假设您可以保证所有日期的格式,以下代码就足够了:
const datetime = '7/14/2016 2:40 PM +00:00'; // this is what your service returns
const pieces = datetime.split(/[/: ]/);
if (pieces[3] == 12) pieces[3] = 0; // fixes edge case for 12 AM/PM
const hours = pieces[5] === 'PM' ? Number(pieces[3]) + 12 : pieces[3];
const d = new Date(Date.UTC(pieces[2], pieces[0] - 1, pieces[1], hours, pieces[4]));
console.log(datetime); // prints "7/14/2016 2:40 PM +00:00"
console.log(d); // prints Date 2016-07-14T14:40:00.000Z
EDIT: There's a couple edge cases with this not handled correctly, namely 12 AM/PM, etc. but those can easily be worked around as well.
编辑:有一些边缘情况没有正确处理,即上午12点/下午等,但这些也很容易解决。
EDIT2: Accounted for that edge case.
EDIT2:占优势。
EDIT3: As a comment stated, this will only work for UTC times. If the string you're receiving can have any offset, this will not work.
EDIT3:正如评论所述,这仅适用于UTC时间。如果您收到的字符串可以有任何偏移量,这将无效。
#3
0
var str = "7\/15\/2016 1:00 AM +00:00".replace("+00:00","UTC");
console.log(new Date(str).toISOString()); // 2016-07-15T01:00:00.000Z
#1
1
There are many ways to parse a string to produce a Date
object.
有许多方法可以解析字符串以生成Date对象。
-
One way is with the
Date
object itself, either by passing a string to the constructor, or by usingDate.parse
. However, only the ISO8601 formats are required in the ECMAScript specification. Any other input is implementation specific, and may or may not be recognized by the different JavaScript runtimes. With web browsers in particular, there are many differences in supported formats across browsers.一种方法是使用Date对象本身,或者通过将字符串传递给构造函数,或者使用Date.parse。但是,ECMAScript规范中只需要ISO8601格式。任何其他输入都是特定于实现的,并且可能会或可能不会被不同的JavaScript运行时识别。特别是在Web浏览器中,跨浏览器支持的格式存在许多差异。
Additionally, the locale of the environment plays a factor in how the values are parsed. How should
1/2/2016
be parsed? January 2nd, or February 1st? You showed an example of7/14/2016
, which would be invalid input if ran in the UK (for example), where the date format isDD/MM/YYYY
.此外,环境的区域设置是解析值的方式的一个因素。应该如何解析1/2/2016? 1月2日还是2月1日?您展示了7/14/2016的示例,如果在英国(例如)运行,则输入无效,其中日期格式为DD / MM / YYYY。
-
You can write custom code to split the string up into its parts, parse each part individually, and compose a result. The main problem with this approach is that the code tends to be rigid, and sometimes fragile. It should be well tested, and many edge cases need to be considered.
您可以编写自定义代码以将字符串拆分为其各个部分,单独解析每个部分并撰写结果。这种方法的主要问题是代码往往是僵化的,有时是脆弱的。它应该经过充分测试,并且需要考虑许多边缘情况。
-
You can use a library, which is by far the easiest and most flexible approach (IMHO). With a good library, you can take comfort in the shared experiences of others, and in the unit tests that are (hopefully) part of the library you choose. Of course, using a library comes with several tradeoffs including increased file size, and relinquishing some degree of control. You should evaluate these tradeoffs carefully.
您可以使用库,这是迄今为止最简单,最灵活的方法(恕我直言)。有了一个好的图书馆,您可以在其他人的共享体验中获得安慰,并且可以在(希望)您选择的图书馆中进行单元测试。当然,使用库有几个权衡,包括增加文件大小,并放弃一定程度的控制。您应该仔细评估这些权衡。
There are many date libraries available for JavaScript. The most popular is probably moment.js, though there are others to choose from, and some larger frameworks sometimes have similar functionality already included.
有许多可用于JavaScript的日期库。最受欢迎的可能是moment.js,尽管还有其他人可供选择,而一些较大的框架有时会包含类似的功能。
Here is an example using moment.js:
以下是使用moment.js的示例:
var i = "7/14/2016 2:40 AM +00:00"; var f = "M/D/YYYY h:mm A Z"; var m = moment(i, f); var o = m.format(f); // will be in the local time, in the same format as the input var d = m.toDate(); // if you really want a Date object
#2
0
Assuming you can guarantee that format for all dates, the following code will suffice:
假设您可以保证所有日期的格式,以下代码就足够了:
const datetime = '7/14/2016 2:40 PM +00:00'; // this is what your service returns
const pieces = datetime.split(/[/: ]/);
if (pieces[3] == 12) pieces[3] = 0; // fixes edge case for 12 AM/PM
const hours = pieces[5] === 'PM' ? Number(pieces[3]) + 12 : pieces[3];
const d = new Date(Date.UTC(pieces[2], pieces[0] - 1, pieces[1], hours, pieces[4]));
console.log(datetime); // prints "7/14/2016 2:40 PM +00:00"
console.log(d); // prints Date 2016-07-14T14:40:00.000Z
EDIT: There's a couple edge cases with this not handled correctly, namely 12 AM/PM, etc. but those can easily be worked around as well.
编辑:有一些边缘情况没有正确处理,即上午12点/下午等,但这些也很容易解决。
EDIT2: Accounted for that edge case.
EDIT2:占优势。
EDIT3: As a comment stated, this will only work for UTC times. If the string you're receiving can have any offset, this will not work.
EDIT3:正如评论所述,这仅适用于UTC时间。如果您收到的字符串可以有任何偏移量,这将无效。
#3
0
var str = "7\/15\/2016 1:00 AM +00:00".replace("+00:00","UTC");
console.log(new Date(str).toISOString()); // 2016-07-15T01:00:00.000Z