0

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.

Zach
  • 23
  • 5
  • 1
    What do you expect `.plusYears()` to do? Where does that come from? And note that `.getUTCDate()` returns a number; a day-of-month value specifically. – Pointy Oct 06 '18 at 15:15
  • When using date-only strings, it's likely that *getUTCDate* will return a different date to the local date where the host is set for a timezone west of Greenwich. Also, 29 Feb 2016 plus 1 year will be 1 March 2017. – RobG Oct 07 '18 at 20:37

2 Answers2

1

plusYear()? where did you get this function from?

try something like

date.setFullYear(date.getFullyear() + i);

should work.

Talg123
  • 1,468
  • 1
  • 10
  • 15
  • Well it should probably only get the year once, or else always add 1. – Pointy Oct 06 '18 at 15:42
  • Right, so `i` goes from 0 to 50. Each time through the loop the year value from the previous iteration is increased by `i`, so it'd go 2018, 2019, 2021, 2024, 2028, etc. – Pointy Oct 06 '18 at 16:20
0

Yes, plusYears isn't a function on Date. I use the method recommended in this question to construct a new date (date = new Date(date.getFullYear() + i, date.getMonth(), date.getDate())). Also, Friday is day 5 (not day 6). See inline comments below:

<script>
  function date() {
    var weekday = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
    var input = document.getElementById("input").value;
    var date = new Date(input);
    console.log("date: " + date);
    for (var i = 0; i < 50; i++) {
      date = new Date(date.getFullYear() + i, date.getMonth(), date.getDate()); // construct new date like this
      console.log("date: " + date);
      if (date.getDay() == 5) { // Friday is 5 (not 6)
        // use date.getDay() to get the correct index of the day name in your week array.
        // Also, append this new value so that it doesn't overwrite the other.
        // You may want to add formatting etc.
        document.getElementById('output').append(document.createTextNode(date + ", " + weekday[date.getDay()]));   
      }
    }
  }
</script>

<form>
  <input type="date" placeholder="dd:mm:yy" id="input" />
  <input type="button" value="weekday" onclick="date()" />
</form>
<p id="output"></p>
slider
  • 12,810
  • 1
  • 26
  • 42