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 ?
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?
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 , sry already and thanks a lot .
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.