Author -  Sai gowtham

How to Solve date.getDay is not a Function in JavaScript

In this tutorial, we are going to learn about how to solve the TypeError: date.getDay is not a function in JavaScript.

When we call a ‘getDay()’ method on a value which is not a data type date object we will get the following error in our console.

Here is an example, how the error occurs:

const date = Date.now();
console.log(date); // unix timestamp

console.log(date.getDay());

Output:

"TypeError: date.getDay is not a function

In the example above, we are getting the error because we are using the ‘getDay()’ method on a integer data type, but the getDay method is only available on a date object.

To solve the “TypeError: date.getDay is not a function”, make sure to call the getDay() method on a data type date object or convert the given value to a valid date object before calling the getDay() method on it.

Here is an example:

const date = new Date();

const result = date.getDay();
console.log(result);

Output:

3

In the example above, we have called the getDay() method a valid date object. So, it returns the integer representation of the current day in the week according to the user local time where 0 represents, 1 represents Monday, etc.

Type checking

To avoid the run time errors, we can also check if the given datatype is a object or not before calling the getDay() method on it.

Here is an example:

const value = new Date();

if(typeof value === 'object' && value !== null && 'getDay' in value){
    console.log(date.getDay());
}else{
    console.log('Given value is not a Date object');
}

Conclusion

The “TypeError: date.getDay is not a function” error occurs, when we call a getDay() method on a value which is not Date object. To solve the error, convert the value to an date object before calling the getDay() method on it or make sure to use the getDay() method on a valid date objects.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY