0

I need to find dinamically given today's date one year ago the same day.

So if today is Monday i need to find the equivalent to today's Monday but one year ago.

Like 03/03/2017 (Friday) would find me 04/03/2017 (Friday). 16/02/2017 (Thursday) would find me 18/02/2017 (Thursday) and so on.

What I am really interested is in finding the day more than the digits. I tried the following:

 Date.prototype.addDays = function (days) {
        var dat = new Date(this.valueOf());
        dat.setDate(dat.getDate() + days);
        return dat;
    }
    var dateObj = new Date();

    var wanted= dateObj.addDays(-365 + 1);

But of course wanted has to change dinamically so this doesn't work. Next I tried finding out the number in the year of that specific Friday in 2017 and then finding that same number in 2017 trying to somehow adapt This example But in this case it only counts the number of fridays not the number and I couldn't get it to work. Maybe the best way would be to be guided by the number of the week and not just the closest Friday to the same number of the date. Is there any way to do this using only JavaScript (without libraries like Moment.js)

Community
  • 1
  • 1
Lima9
  • 49
  • 2
  • 9

2 Answers2

2

You can try something like this:

Logic:

  • Take number of years +/- to go backward/forward.
  • Use date.setFullYear to update date.
  • Now check difference between day of both dates.
  • Add this difference to date and return it

function getSameDayOfYear(date, yearsToAdd){
  date = typeof(date) === "string" ? new Date(date) : date;
  var retDate = new Date(+date);
  retDate.setFullYear(retDate.getFullYear() + yearsToAdd);
  
  var diff = date.getDay() - retDate.getDay();
  retDate.setDate(retDate.getDate() + diff);
  console.log(retDate.toLocaleDateString())
  return retDate;
}

getSameDayOfYear("03/03/2017", -1)
getSameDayOfYear("02/16/2017", -1)

getSameDayOfYear("03/03/2017", -2)

As correctly pointed out by @brieucdlf, setYear has been deprecated. Its better to use setFullYear

Reference: setYear - MDN

Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • Thank you very much. Your approach is very interesting! – Lima9 Mar 03 '17 at 13:11
  • 1
    setDate is deprecated. Use setFullYear instead. https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Date/setYear – brieucdlf Aug 31 '22 at 15:19
  • @brieucdlf Thanks for highlighting. This is a 5 year old answer, so I'll update it with newer version – Rajesh Sep 02 '22 at 01:59
1

There is a slight problem in Rajesh's code. It will go backward 5 or 6 days when it would be "closer" to go forward 1 or 2 instead.

Here is a simple fix:

function getSameDayOfYear(date, yearsToAdd){
  date = typeof(date) === "string" ? new Date(date) : date;
  var retDate = new Date(+date);
  retDate.setYear(retDate.getFullYear() + yearsToAdd);
  
  var diff = date.getDay() - retDate.getDay();
  if(diff < -4){
    diff = diff + 7; //go forward instead of backward
  }

  retDate.setDate(retDate.getDate() + diff);
  console.log(retDate.toLocaleDateString())
  return retDate;
}