When we say new Date()
we are essentially creating a new instance/object of the class Date
using Date()
constructor method. When we call the Date()
method without the use of new
keyword it actually returns a String not an instance/object of the class Date
. And a string will not contain the method getFullYear();
. Hence we get an error
Now consider the below code snippet:
let dateTimeNowObj = new Date(); // returns a object of class Date
console.log(dateTimeNowObj) // Sat Jun 14 2025 23:48:27 GMT+0530 (India Standard Time)
console.log(dateTimeNowObj.getFullYear()); // 2025
let dateTimeNowStr = Date(); // returns a string
console.log(dateTimeNowStr) // Sat Jun 14 2025 23:47:32 GMT+0530 (India Standard Time)
console.log(dateTimeNowStr.getFullYear()); // TypeError: dateTimeNowStr.getFullYear is not a function