简体   繁体   English

Javascript如何将非UTC日期字符串转换为Date对象?

[英]Javascript How to convert a non-UTC Date string into the Date object?

I have a string "2017-01-05T15:03:25.21" which is already the exact time in my timezone. 我有一个字符串“2017-01-05T15:03:25.21”,这已经是我时区的确切时间。 (eg: +8) How to convert this string into the Date Object? (例如:+8)如何将此字符串转换为Date对象? The reason of asking this is that, the Date class seems to accept a 'UTC Date String' only. 问这个的原因是,Date类似乎只接受'UTC Date String'。 If i directly do this: 如果我直接这样做:

var strDateTime = "2017-01-05T15:03:25.21";
var myDate = new Date(strDateTime);

//myDate will have another redundant time-zone offset.

How to convert this properly? 如何正确转换?

You can add timezone to end of string like this: 您可以将时区添加到字符串结尾,如下所示:

var strDateTime = "2017-01-05T15:03:25.21"+"-08:00";
var myDate = new Date(strDateTime);

You can read more about valid date format here: 您可以在此处详细了解有效日期格式:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

https://www.w3.org/TR/NOTE-datetime https://www.w3.org/TR/NOTE-datetime

How to convert this properly? 如何正确转换?

Implementations consistent with ECMAScript ed 5 (ES5) and later will parse the string correctly with either the Date constructor or Date.parse, so: 与ECMAScript ed 5(ES5)及更高版本一致的实现将使用Date构造函数或Date.parse正确解析字符串,因此:

var strDateTime = "2017-01-05T15:03:25.21";
var myDate = new Date(strDateTime);

Will produce a date for 5 Jan 2017 3:03:25.21 PM in the local timezone. 将于2017年1月5日3:03:25.21 PM在当地时区生成日期。

However, it's not recommended to do that given the general inconsistencies in parsing between implementations. 但是,鉴于实现之间的解析一般不一致,不建议这样做。 Either a custom function or library should be used, eg using fecha.js : 应该使用自定义函数或库,例如使用fecha.js

var myDate = fecha.parse(strDateTime, "YYYY-MM-DDTHH:mm:ss.SS")

From ES5 and later, ISO 8601 date and time strings are mostly parsed as specified in ISO 8601. The main differences are: 从ES5及更高版本开始,ISO 8601日期和时间字符串主要按照ISO 8601中的规定进行解析。主要区别在于:

  1. Date–only forms like "2016-12-12" are parsed as UTC rather than local 像“2016-12-12”这样的仅日期表单被解析为UTC而不是本地
  2. Only a " simplification of the ISO 8601 Extended Format " is supported, as detailed in ECMA-262 §20.3.1.6 Date Time String Format 仅支持“ ISO 8601扩展格式的简化 ”,详见ECMA-262§20.3.1.6日期时间字符串格式

So given: 所以给出:

var s = "2017-01-05T15:03:25.21";
var d = new Date(s)

then d will be converted to a Date with a UTC time value based on the host system timezone setting. 然后, d将根据主机系统时区设置转换为具有UTC时间值的日期。 If your host is set to UTC+0800, then that is the timezone that is applied. 如果您的主机设置为UTC + 0800,那么这是应用的时区。

Date objects do not have a time zone. 日期对象没有时区。 All they have is a single time value that represents milliseconds since 1970-01-01T00:00:00Z. 它们只有一个时间值,表示自1970-01-01T00:00:00Z以来的毫秒数。 Any related time zone information comes from the host. 任何相关的时区信息都来自主机。 So the comment: 所以评论:

//myDate will have another redundant time-zone offset.

is incorrect. 是不正确的。 The host time zone setting will be used to determine the offset that is used to calculate the UTC time value, and also to calculate the local values returned by the get* methods like getFullYear , getMonth , getDate , getHours , etc. 主机时区设置将用于确定用于计算UTC时间值的偏移量,还用于计算get *方法返回的本地值,如getFullYeargetMonthgetDategetHours等。

The string "2017-01-05T15:03:25.21" on a system with an offset of +0800 will generate a time value of 1483599805210, which represents 2017-01-05T07:03:25.21Z (ie UTC+0000). 偏移量为+0800的系统上的字符串“2017-01-05T15:03:25.21”将生成时间值1483599805210,表示2017-01-05T07:03:25.21Z(即UTC + 0000)。

If you wish to send a timestamp that represents a particular moment to clients in different time zones, then one of the following should suite. 如果您希望向不同时区的客户端发送代表特定时刻的时间戳,则应满足以下条件之一。

Append the time zone to the string: 将时区附加到字符串:

2017-01-05T15:03:25.21+0800

Use the UTC equivalent: 使用UTC等效项:

2017-01-05T07:03:25.21Z

Use a time value: 使用时间值:

1483599805210

The last is generally preferred as new Date(timevalue) is supported by all implementations, is unambiguous and can be easily converted for use in other systems. 最后一个通常是首选的,因为所有实现都支持new Date(timevalue) ,是明确的,并且可以很容易地转换以便在其他系统中使用。

This should work 这应该工作

var strDateTime = "2017-01-05T15:03:25.21";
var myDate = new Date(strDateTime.replace('T', ' '));

OUTPUT for my timezone : Thu Jan 05 2017 15:03:25 GMT+0200 (EET) 输出我的时区:2017年1月5日星期五15:03:25 GMT + 0200(EET)

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

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