I want to make a place where you key in your next birthday and the page does a window.alert
to tell you how many days are left for your birthday. When I run the code, before I key in the birthday, the alert says NaN which means not a number. I want it to work after I key in the birthday after I click submit. This is what I coded:
`
<input type="submit" value="Submit">
</form>
<script>
let date_1 = new Date(document.getElementById("bday").value);
let date_2 = new Date();
let difference = date_1.getTime() - date_2.getTime();
let TotalDays = Math.ceil(difference / (1000 * 3600 * 24));
window.alert(TotalDays);
</script>
</body>`
2
Answers
I see basically two problems here.
First of all, the value that you are getting from bday is going to be a string, so you should be sure of the format of that value. You need to get the day, the month and the year, in order to create a string like "day/month/year", to convert it into a date type, using Date.parse().
Secondly, to get the actual date, you should use Date.now(). The constructor alone will not work.
Apllying these changes we have:
Have a nice day!