Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - difference between Date(), new Date(), and ISODate

I've seen various versions of this question, but none of them answer my needs.

I want to create an ISODate for MongoDB and I'm using Node.js.

In Node, when I do:

console.log(Date());

I get:

Mon Sep 26 2016 15:17:04 GMT-0400 (EDT) <-- This is correct.

When I do:

console.log(new Date());

I get:

2016-09-26T19:17:04.731Z <- This is 4 hours ahead

My understanding of the way to do ISODATE is:

var isodate = new Date().toISOString()
console.log(isodate);

Which yields a time 4 hours ahead of "now".

My system date is correct. I run this one different machines, and I get the same results.

Can someone please explain why I'm getting a discrepancy in time?

like image 631
AqC Avatar asked Sep 26 '16 19:09

AqC


People also ask

What is the difference between date and new date in Javascript?

Date() returns an implementation dependent string representing the current date and time. new Date() returns a Date object that represents the current date and time. ;-) Yeah, I know.

What is Isodate in Javascript?

The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ , respectively). The timezone is always zero UTC offset, as denoted by the suffix Z .

What is the difference between toISOString and toUTCString?

toISOString() provides millisecond values, whereas . toUTCString() does not.

How do I convert to Isodate?

toISOString() method is used to convert the given date object's contents into a string in ISO format (ISO 8601) i.e, in the form of (YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss. sssZ). The date object is created using date() constructor.


3 Answers

The difference is that 2016-09-26T19:17:04.731Z related to GMT0 timezone and Mon Sep 26 2016 15:17:04 GMT-0400 (EDT) to your local timezone. Both are point to the same time :)

You can read more about data formats and timezones in Wiki

like image 192
AlexeyTokar Avatar answered Oct 16 '22 21:10

AlexeyTokar


With a basic definition to the difference between Date() and new Date() is :

  • Date() ignores any argument(s) passed to it and is equivalent of new Date().toISOstring()

  • new Date(Optional_arguments) creates an time type object in JS on which you can perform :

  • getTime() other Date.prototype functions listed on MDN Website

  • Date() is just a string representation of local time.

    • new Date() gives you a manipulatable object to fiddle around.
like image 26
mannutech Avatar answered Oct 16 '22 21:10

mannutech


Notice the Z at the end of 2016-09-26T19:17:04.731Z? It stands for Zulu, meaning UTC timezone (which is GMT+000).

As you can see in your original date string, Mon Sep 26 2016 15:17:04 GMT-0400 (EDT) has a GMT-0400 timezone, which I guess is the local time where you live. So, in fact there is no problem, just different representations of the same time:

  • Date() creates a Local date
  • new Date() creates a UTC date
like image 32
eavidan Avatar answered Oct 16 '22 21:10

eavidan