Javascript How to convert a non-UTC Date string into the Date object? - Stack Overflow

I have a string "2017-01-05T15:03:25.21" which is already the exact time in my timezone. (eg:

I have a string "2017-01-05T15:03:25.21" which is already the exact time in my timezone. (eg: +8) How to convert this string into the Date Object? The reason of asking this is that, the Date class seems to accept a 'UTC Date String' only. 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?

I have a string "2017-01-05T15:03:25.21" which is already the exact time in my timezone. (eg: +8) How to convert this string into the Date Object? The reason of asking this is that, the Date class seems to accept a 'UTC Date String' only. 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?

Share Improve this question asked Jan 5, 2017 at 7:15 zeroflawzeroflaw 5742 gold badges13 silver badges25 bronze badges 5
  • You could use moment.js or other dedicated library - it's much easier than having to do it manually, because JS at the moment has poor native support of date-time logic. – Ovidiu Dolha Commented Jan 5, 2017 at 7:18
  • please accept my answer as correct if it's helpful :) – Lukas Liesis Commented Jan 5, 2017 at 7:24
  • @Ovidiu Dolha thanks for mentioning this, as i thought i was missing sth. – zeroflaw Commented Jan 5, 2017 at 7:27
  • @Lukas Liesis, i have upvoted :) let me observe a while. – zeroflaw Commented Jan 5, 2017 at 7:27
  • In general, you should never parse strings with the Date constructor (or Date.parse, they are equivalent for parsing). From ES5 onward, ISO 8601 format strings are usually correctly parsed with the exception of the date only form, where parsing is inconsistent with ISO 8601: it's treated as UTC rather than local. To be safe, always manually parse strings (a library can help, but a bespoke function need only be 3 or 4 lines of code). – RobG Commented Jan 5, 2017 at 22:40
Add a ment  | 

3 Answers 3

Reset to default 2

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:

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.

However, it's not remended to do that given the general inconsistencies in parsing between implementations. Either a custom function or library should be used, e.g. using 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:

  1. Date–only forms like "2016-12-12" are parsed as UTC rather than local
  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

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. If your host is set to UTC+0800, then that is the timezone that is applied.

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. Any related time zone information es from the host. So the ment:

//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.

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 (i.e. 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:

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.

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/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

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

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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745252236a4618746.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信