📋 Table Of Content
The error "date.getDate() is not a function" occurs when we call the method (
getDate()) on a value which is not a valid date object. To solve this error, we have to make sure that the method is called only on valid date objects.
Let's see a demonstration of how this error might occur.
const date = '2022-09-04' console.log(date.getDate())
This will give us an error like this.
TypeError: date.getDate is not a function
This is because even though the date looks like it's in the correct date format, it actually just a string.
So we cannot use the
getDate() method on a date string.
So how to solve the "date.getDate() is not a function" error?
To use the
getDate() method on the above date string, we have to first convert it into a valid date object.
To convert we have to use the
const date = '2022-09-04' const validDate = new Date(date) console.log(validDate.getDate()) // 4
Here, we have converted the date ('2022-09-04') into a valid Date object using
new Date() constructor, and then called the
getDate() method on it.