1

Currently the time on my machine is 07:55

If I console.log(new Date()) in my vscode terminal then I get the wrong time of 05:55. This is because my time zone (South Africa) is +2. So in vscode / node my new Date() is showing the universal time, not my local time.

If I console.log(new Date()) in a .html file (if I run it in Chrome) then I get the correct time of 07:55.

Is the problem in node, vscode or somewhere else? And how can I fix this problem so that my vscode/node console.log's the correct time.

console.log(new Date().getHours()) does indeed give me the correct hour of 7, but I don't understand why console.log(new Date()) gives me the wrong hour of 2019-09-11T05:55:00.480Z

console.log(new Date().toLocaleString()) does indeed give me the correct time, but I don't want that format. I just want the basic new Date() format for database purposes.

// 2019-09-11T05:55:00.480Z
console.log(new Date())    

// 9/11/2019, 7:55:00 AM
console.log(new Date().toLocaleString())
Paul Kruger
  • 2,094
  • 7
  • 22
  • 49
  • "I just want the basic new Date() format" — There isn't a "basic new Date() format". Generate the explicit format you want using the features of the Date class. – Quentin Sep 11 '19 at 06:08
  • @Quentin Well I am just trying to make a point that `.toLocaleString()` does indeed give me the correct hours, but it is no longer a date object I can manipulate. Point is: why can't new Date() just be correct, or how can I fix it. – Paul Kruger Sep 11 '19 at 06:14
  • It *is* correct. It will have the time zone information in it. – Quentin Sep 11 '19 at 06:15
  • Ok, then how do I `console.log()` the non-timezone `new Date()` in node? I literally just want the hours to look the same for a front end user. – Paul Kruger Sep 11 '19 at 06:17

2 Answers2

2

You can do new Date().toString() in node to get the format from chrome

node:

console.log(new Date()); // 2019-09-11T06:25:01.665Z
console.log(new Date().toLocaleString()); // 9/11/2019, 1:25:01 PM
console.log(new Date().toString()); // Wed Sep 11 2019 13:25:01 GMT+0700 (GMT+07:00)
Karthik
  • 109
  • 5
  • 1
    The output of `console.log(dateObj)` and *toLocaleString* are implementation dependent. The output of *toString* is defined in [*ECMA-262*](http://ecma-international.org/ecma-262/10.0/#sec-date.prototype.tostring). – RobG Sep 11 '19 at 12:50
1

node prints the ISO string by default and chrome prints the adjusted time. The timestamp is still the same in both the cases. It will be saved as the same in both the cases in a database.

You can verify this by doing new Date().toISOString() in chrome to see the same output as of node and also check new Date().getTime() in both chrome and node to see they are the same

Karthik
  • 109
  • 5
  • Ok that makes sense, but is there no way to adjust `new Date()` to bring back the non-ISO time? – Paul Kruger Sep 11 '19 at 06:16
  • What do you want to store in the database? A timestamp or the date as string? – Karthik Sep 11 '19 at 06:22
  • You can use `new Date().toString()` in `node` to get chrome format. – Karthik Sep 11 '19 at 06:25
  • @Karthik—timestamps mostly are strings. 2019-09-11T06:25:01.665Z, 9/11/2019, 1:25:01 PM and Wed Sep 11 2019 13:25:01 GMT+0700 (GMT+07:00) are all timestamps. Your second "answer" should have been an edit of your first answer. – RobG Sep 11 '19 at 12:52