Shilpa
2 Mar 2022
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`.
Rakshit
5 Mar 2022
Best Answer
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.
© 2024 Copyrights reserved for web-brackets.com