user image

Shilpa
Published in : 2022-03-02

javascript: Uncaught TypeError: undefined is not a function

Javascript

What did I miss?

var userJoinedDate = new Date().setDate(3);
document.getElementById("result").innerHTML = userJoinedDate.getDate();

it returns error `Uncaught TypeError: undefined is not a function`.

Comments

Rakshit Date : 2022-03-05

Best answers

34

Best answers

34

Reason for issue: When you use setDate, it modified the object and so it will return undefined, as it wasn't defined internally.

When you change the day, you could copy userJoinedDate object and then change it to the anotherDate name.

var userJoinedDate = new Date();
var anotherDate = new Date(userJoinedDate.getTime());
anotherDate.setDate(3);
document.getElementById("result").innerHTML = anotherDate.getDate();

Try in this way, it will never give undefined error.

Shilpa Date : 2022-03-06

Best answers

10

Best answers

10

Thanks, It worked! Never thought of such a solution!

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

JavaScript variable for Changing CSS Height
Publish date: 2022-02-12 | Comments: 1

Tag: Javascript

Check if div have touch another div
Publish date: 2022-03-02 | Comments: 1

Tag: Javascript

How to open URL in new tab & focus on it in Javascript?
Publish date: 2021-12-22 | Comments: 1

Tag: Javascript

How can I switch between two background-colors using js setinterval( )
Publish date: 2022-02-22 | Comments: 1

Tag: Javascript

Can we add a javascript or jQuery to the URL?
Publish date: 2022-03-05 | Comments: 2

Tag: Javascript

Simple HTML date picker is showing not showing calendar icon!
Publish date: 2022-03-01 | Comments: 2

Tag: Javascript