0

From the server I get a datetime serialized object. (2016-11-03T10:51:01.517)

When I do

var d = new Date("2016-11-03T10:51:01.517")

d is now Thu Nov 03 2016 06:51:01 GMT-0400 (Eastern Daylight Time)

I need the time to be exactly 10:51:01.

Thanks in advance

martincarlin87
  • 10,848
  • 24
  • 98
  • 145

2 Answers2

1

It seems to be a timezone error.

  • You mean it's a parsing error. – RobG Feb 18 '19 at 05:20
  • Not error. It is a normal behaviour of date parsing in JS. E.g., check what happens adding a "Z" after datetime string (`new Date( '2016-11-03T10:51:01.517Z' )`). – Miquel Al. Vicens Feb 19 '19 at 07:44
  • Yes, an error. "2016-11-03T10:51:01.517" **should** be parsed as local, but for the OP it's being parsed as UTC. "Normal behaviour" doesn't count, the [*language specification*](http://ecma-international.org/ecma-262/9.0/#sec-date-time-string-format) does. If "normal" is what most browsers do, then it's not normal either. Firefox, Opera and Chrome get it right (local), Safari gets it wrong (UTC). As always: do not trust the built–in parser. ;-) – RobG Feb 19 '19 at 09:17
  • My testing browser is Firefox. In my first answer, when I said "error" I mean this is a bad assimilation of questioner. Sorry for the misunderstanding. – Miquel Al. Vicens Feb 19 '19 at 09:54
0

You can seeing the time in the local time zone format. Use toUTCString() method to get the time as you desired.

var d = new Date("2016-11-03T10:51:01.517")    
alert(d.toUTCString());

d.toUTCString() will return us Thu, 03 Nov 2016 10:51:01 GMT

  • Which is wrong. "2016-11-03T10:51:01.517" *should* be parsed as local as there is no timezone offset in the timestamp. The UTC equivalent should be different by the timezone offset, not the other way around. ;-) – RobG Feb 18 '19 at 05:18