2

I'm sorry but the new Date() is not returning an object as I understand an object.

From the MDN article on Date constructor :

When no parameters are provided, the newly-created Date object represents the current date and time as of the time of instantiation.

(emphasis added)

All right then, where is the object there :

const date1 = new Date();
console.log(date1);
// returns Wed Jan 27 2021 15:45:31 GMT+0100

An object would be

{date: "Wed Jan 27 2021 15:45:31 GMT+0100")}

But it looks more like a string, BUT we can't manipulate it like a string, since :

const date1 = new Date();
console.log(date1[0]); // expecting "W"
// returns undefined

I know how to manipulate this special format with the different time methods but this has always been a mystery for me.
What is this format ?
Another Type ?
And why there is no object returned by the constructor new Date()?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
J.doe
  • 115
  • 10
  • 3
    `date1` *is* an object. Your console just shows the date it contains for convenience. Try `typeof date1 === "object"`, `date1 instanceof Object`, and `date1 instanceof Date` – VLAZ Jan 27 '21 at 14:52
  • Try `console.log(typeof new Date());`. It's an Object. – esqew Jan 27 '21 at 14:52
  • Yes sure I've already typofed the value, but why showing the value of the object for "convenience" ? It's not convenient lol, what is the name of the property that store my date for example ? – J.doe Jan 27 '21 at 14:53
  • Why not do `console.dir(new Date())` for a more accurate representation of the Object's structure? Even further, why not refer to the [documentation for `Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) to find out "*what is the name of the property that store my date*"? "*It's not convenient*" I'm sure those involved in defining the ECMAScript standard would disagree with your opinion. – esqew Jan 27 '21 at 14:54
  • I went to the doc before asking and the name of the property is not shown, or at least not in the first pages, and console.dir(new Date()) is not showing a property either. Also why it is nowhere written that we are not showing the object of new Date() for convenience ? – J.doe Jan 27 '21 at 14:57
  • 1
    You are thinking about the Date object all wrong. There is no property that stores your date. The Date *is* your date. As with other objects, you can use the `valueOf()` method to get a primitive representation of the object. In the case of Date, it is a number indicating the number of milliseconds since the UNIX epoch. – Heretic Monkey Jan 27 '21 at 14:58
  • 1
    There is a setting called DontEnum for date: https://stackoverflow.com/questions/2257993/how-to-display-all-methods-of-an-object – mplungjan Jan 27 '21 at 14:58
  • @J.doe "*but why showing the value of the object for "convenience"*" there is no spec for consoles. When they receive an object, they are free to presented as they want. Many will take a special case for Date objects because it *is* convenient. The vast majority of use cases, you print it because you want to know what the content is. Even if there is no special exception for Dates, consoles still often respect the `toString()` method. – VLAZ Jan 27 '21 at 14:58
  • `(""+date1)[0]` will return "W" – mplungjan Jan 27 '21 at 15:04
  • All right, so there is no actual way to represent that new Date() object in the console ? I mean with property and everything. – J.doe Jan 27 '21 at 15:14

2 Answers2

2

When calling new Date(), you will get a date object. When calling console.log(new Date()), the toString-method of the date object will implicitly get called (not 100% true, but you get the idea). How the console-object works, is implemented by your browser/enironment. My guess is showing the date as a string is a convenience they've build in.

Like others have mentioned in the comments, try console.log(typeof new Date())/console.dir(new Date()) or to call any of the methods documented. You'll see it is most definitely an object.

Thijs
  • 3,015
  • 4
  • 29
  • 54
  • Hey thank's for commenting, it's kind of crazy that they implicity call toString just for "new Date()". Also if they call toString() with it, why can't I manipulate it like a string ? They just call it for the console ? I've already tried typeof and dir before commenting, but it's not helping to truly understand what is going on. I still haven't found a way to truly visualize the newly created object(but I have other responses to read). – J.doe Jan 27 '21 at 15:07
  • @J.doe "*Also if they call toString() with it, why can't I manipulate it like a string ?*" because **you** aren't calling `toString()` on the object. That is (potentially) only called by the console before printing it. – VLAZ Jan 27 '21 at 15:11
  • Sigh, that thing must have confused a lot of people ... (so there is no way to actually see that new Date() object in the console?) – J.doe Jan 27 '21 at 15:13
  • @J.doe depends on the environment you use and the console you use. Most likely the answer is no. You could try getting its prototype or its method but I'm not sure if that's what you're after. – VLAZ Jan 27 '21 at 15:16
0

While there is a string returned by the "console.log(date)", the "date" variable itself is actually an object. It's just there so that if you wanted to print out the date quickly, then you would do "console.log(date)". However,for this you cannot do any manipulations. You would have to look at the methods of the "Date" class. There are many other functions within it that allow you to get specific information.

For example:

date.getMonth()
date.getDay();
date.getYear();

This Date constructor has functions of it's own which make it easier. So, you don't have to reference the values of the object itself. All you have to do is type these commands at it should automatically give you the information you need.

This documentation is available at : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Just scroll down to "INSTANCE METHODS" and all the methods should be available there.

SatvikVejendla
  • 424
  • 5
  • 14
  • In the question he asked "What is this format ?" so I used these to give an example of how he would be able to access the info. Plus he said "BUT we can't manipulate it like a string,", so he was obviously trying to use the information from the Date object and get specific info about it, such as month. – SatvikVejendla Jan 27 '21 at 14:57
  • Thank you for your comment, but actually I was trying to visualize the object returned by new Date(). I know those methods, they are pretty handy indeed, but it's so confusing that they are saying that new Date() returns an Object, but they actually show a string in the console BUT you can't manipulate this string directly, you need those Date methods ... – J.doe Jan 27 '21 at 15:11
  • Ok. No problem. Guess I just misunderstood your question – SatvikVejendla Jan 27 '21 at 15:20