Accesing Date object property values

Hey, how its going ?

So I was learning about objects and prototypes at the MDN javascript course, and I tried to identify a new object:
const myTime = new Date();
when i write myTime in the console it gives me this msg
Date Mon Oct 17 2022 00:14:54 GMT-0700 (Pacific Daylight Time)
But when i try to bring the value of the property “Date” ( myTime.Date ) its not working .
Am I missing something ? :dizzy_face:

Hi @andernader and welcome to the community :wave:

By using new you’re creating a date object with the current date and time. So myTime itself is a date object and you can call various instance methods on it described here:

Could you explain what your goal is or what you did expect by using myTime.Date?

Have a nice day,
Michael

1 Like

Hey Michael
mm I was just learning about objects so u know u can call an object like for example “the name property inside a person object” by preson.name i tried the same by calling the date property inside myTime object .
i defined my object const myTime = new Date(), I called it and got this message : Date Tue Oct 18 2022 15:24:34 GMT-0700 (Pacific Daylight Time)
so is Date here a property of myTime ? if not then this whole value is regestired inside which property ?
I know this question might sounds kinda stupid :dizzy_face: , sry already :blossom: and thanks a lot .

I don’t think that sounds stupid at all :blush:

myTime itself is a date object after you use new Date(). When you console.log() an object than its toString() function gets called. Date objects happen to have a special toString() function that prints the string you saw: Date.prototype.toString().

Since myTime is a date object, you can also call all the other Date methods like:

  • myTime.getFullYear()
  • myTime.setHours()
  • and many more (See my link in the last post for a full list)

I recommend having a look at the examples in my last post’s link.

Does that make it a bit clearer?