I've done a bit of searching and either haven't found an example that fits, or perhaps I just can't quite grasp some of concepts yet.
I'm trying to write a function that lets the user input a date (from a form) and return every year where the date falls on a Friday over the next 50 years. I'm sure there are several things wrong with my initial approach, but my primary concern is the .plusYears() function is not working. Thanks for any feedback!
<script>
function date() {
var weekday = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
var input = document.getElementById("input").value;
var date = new Date(input).getUTCDate();
console.log("date: " + date);
for (var i = 0; i < 50; i++){
date = date.plusYears(i);
console.log("date: " + date);
if(date.getDay() == 6){
document.getElementById('output').textContent = date.getDate() + ", " + weekday[date];
}
}
}
</script>
<form>
<input type="date" placeholder="dd:mm:yy" id="input" />
<input type="button" value="weekday" onclick="date()" />
</form>
<p id="output"></p>
EDIT:
function date() {
var input = document.getElementById("input").value;
var date = new Date(input);
for (var i = 0; i < 50; i++) {
var y = 1;
date = new Date(date.getFullYear() + y, date.getMonth(), date.getDate());
if(date.getDay() == 5){
console.log("friday" + date);
}
else{
console.log("other day");
}
}
}
Unsure why the console is displaying the date prior to whichever the user inputs.