2

I have a datetime string and want to create a JS Date Object:

var dt_string = "2/10/2018 11:52:41 PM";
var d_object = new Date(dt_string);

I want to print the date/time in a different format (which just so happens to match the ISO String):

console.log("date/time: " + d_object.toISOString().substring(0,10) + " " + d_object.toISOString().substring(11,19));

I expect to see:

"2018-02-10 23:52:41"

Instead I get this:

"2018-02-11 04:52:41"

Which is 5 hours later than it's supposed to be. After debugging, I found this:

new Date(d_string): Sat Feb 10 2018 23:52:41 GMT-0500 (Eastern Standard Time)

Which makes me think it's adding 5 hours to my time to match GMT. How do I get JS to respect my original time, and not change it?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Travis Heeter
  • 13,002
  • 13
  • 87
  • 129
  • That's what's supposed to happen. JavaScript's `new Date` parses the date assuming it's local, but `toISOString` returns it in ISO8601 format, which is in UTC/"Zulu" (hence the trailing `Z`). Frankly you're lucky it was that close, the date you're providing is *clearly* in October, not February, 2018! – jonrsharpe Feb 13 '18 at 22:51
  • `toISOString()` outputs UTC date - if you didn't chop bits out of it, you'd see that – Jaromanda X Feb 13 '18 at 22:51
  • I updated my answer to actually provide a simple solution to your question and explain the behaviour you observed. Hope it helps – Paolo Feb 17 '18 at 16:27

2 Answers2

3

First of all you should make your life simple and install moment-timezone.

However, as comments have suggested, toISOString uses the UTC form.

If you really care about printing in your timezone, check out this StackOverflow question.

Here is a manipulation of one of those answers to print what you wanted. Definitely not the ideal method.

var dt_string = "2/10/2018 11:52:41 PM";
var d_object = new Date(dt_string);
console.log(d_object);

d_object.setTime(d_object.getTime() - d_object.getTimezoneOffset() * 60 * 1000);
console.log(d_object);
Matt Goodrich
  • 4,875
  • 5
  • 25
  • 38
  • 1
    This solution lead me to the my answer. For my situation I need to add the timezone, as in: d_object.setTime(d_object.getTime() + d_object.getTimezoneOffset() * 60 * 1000); console.log(d_object); – Morgan Hayes Feb 20 '18 at 00:54
0

It is assumed that every Date object's timezone is the local machine's timezone: UTC-05 in your case.

As toISOString() returns a zero UTC offset date (in the form of a string) then you get 5 hours added.

Note that the Date object doesn't store internally any timezone information.

In order to answer your question:

How do I get JS to respect my original time, and not change it?

If your "original time" is meant to be zero UTC offset then you can specify that in the string passed to Date constructor:

var dt_string = "2/10/2018 11:52:41 PM UTC+0000";
//                                     ^^^^^^^^
var d_object = new Date(dt_string);

Now if you evaluate:

console.log("date/time: " + d_object.toISOString().substring(0,10) + " " + d_object.toISOString().substring(11,19));

You get:

"date/time: 2018-02-10 23:52:41"

Note that according to what previously written if you inspect d_object you'll get the parsed date converted to your local timezone.

Paolo
  • 15,233
  • 27
  • 70
  • 91