简体   繁体   English

将JavaScript ISO 8601字符串转换为Date对象

[英]JavaScript ISO 8601 string into Date object

Consider the following two snippets in a browser's JavaScript console (I have tried with Google Chrome) 在浏览器的JavaScript控制台中考虑以下两个片段(我曾尝试使用Google Chrome浏览器)

1st Statement: 第一条声明:

s = "2014-03-03 18:30:00";
d = new Date(s);
// Mon Mar 03 2014 18:30:00 GMT+0100 (CET)

2nd Statement: 第二条陈述:

s = "2014-03-03T18:30:00";
d = new Date(s);
// Mon Mar 03 2014 19:30:00 GMT+0100 (CET)

See? 看到? The parsed date and time differs with one hour for me, as I live in UTC+1. 因为我居住在UTC + 1,所以解析的日期和时间对我来说是一个小时。

But why does the JavaScript Date object parse these two strings differently, for there is no time zone given at all? 但是为什么JavaScript Date对象解析这两个字符串的方式不同,因为根本没有给出时区?

  • The 2014-03-03T... notation is a fancy JavaScript Date Time String Format and expects a time zone. 2014-03-03T...表示法是一种花哨的JavaScript日期时间字符串格式 ,需要一个时区。 If you don't provide one, it defaults to Z (UTC). 如果您不提供,则默认为Z (UTC)。

  • The 2014-03-03 18:30:00 notation, however, is just a regular string without an interesting name and, if you don't provide a time zone, it assumes local time. 但是, 2014-03-03 18:30:00表示法只是一个常规字符串,没有一个有趣的名称,并且,如果您不提供时区,它将采用当地时间。

This info was taken from the MDN article about Date.parse() . 此信息来自有关Date.parse()MDN文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM