3

I am trying to get the last Saturday from a given date. If function argument is 9/12/2019 it should return 9/7/2019.

I have tried two methods below with no success:

function getLastSaturday(theDate) {
  debugger
  var dateToUse = new Date(theDate);
  var lastSaturday = new Date(new Date().setDate(dateToUse.getDate() - (dateToUse.getDay() == 0 ? 7 : dateToUse.getDay() + 1)));
  return lastSaturday;
}

function getLatestSaturday(theDate) {
  var dateToUse = new Date(theDate);
  var latestSaturday = new Date(new Date().setDate(dateToUse.getDate() - dateToUse.getDay() + 1));
  return latestSaturday;
}

console.log(getLastSaturday('9/12/2019'));
console.log(getLatestSaturday('9/12/2019'));

console.log(getLastSaturday('8/5/2019'));

Update

Modified getLastSaturday(theDate) that works

function getLastSaturday(theDate) {
    var dateToUse = new Date(theDate);
    var start = dateToUse.getDay() == 0 ? 7 : dateToUse.getDay();
    var target = 6; // Saturday

    if (target >= start)
        target -= 7;
    var lastSaturday = dateToUse.addDays(target - start);
    return lastSaturday;
}

Date.prototype.addDays = function(days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
}
NoBullMan
  • 2,032
  • 5
  • 40
  • 93
  • Your first example works fine. – Rory McCrossan Sep 12 '19 at 16:04
  • when I test it, I select 8/5/2019 and it return 9/3/2019. The date that I get from an Ajax call and pass to this function is of the form `Mon Aug 05 2019 00:00:00 GMT-0400 (Eastern Daylight Time)` i.e. the parameter `theDate` – NoBullMan Sep 12 '19 at 16:06
  • Have you seen this post? https://stackoverflow.com/questions/3638906/get-date-of-specific-day-of-the-week-in-javascript/3639224 wouldn't be too difficult to modify one of the answers to fit your needs. – user7290573 Sep 12 '19 at 16:35
  • Helpful, I will try to see if I can use DateJS library. I think my problem is the format of the date I get from Ajax; if i can convert `Mon Aug 05 2019 00:00:00 GMT-0400 (Eastern Daylight Time)` to `08/05/2019` I think I can use my first function – NoBullMan Sep 12 '19 at 17:56
  • 1
    I was able to use DateJS library to resolve this, thanks to @user7290573. Makes it very simple, all I had to do was take the date returned from Ajax call (`dtWeekStart`) and use `dtStart = dtWeekStart.last().saturday();`. If you make your comment into a response, i will mark it as answer. – NoBullMan Sep 12 '19 at 19:13
  • 1
    I have also updated my first JS function to return the same result although DateJS is much easier. – NoBullMan Sep 12 '19 at 19:26
  • I think you should post an answer to your own question as it sounds like you've found a couple of solutions that worked for you. Glad to have pointed you in the right direction, though! – user7290573 Sep 12 '19 at 19:59

1 Answers1

2

Consider the line:

var lastSaturday = new Date(new Date().setDate(dateToUse.getDate() - (dateToUse.getDay() == 0 ? 7 : dateToUse.getDay() + 1)));

This gets the current date, then subtracts the day number of some other date. All you need to do is subtract the date's day number + 1, e.g.

function getLastSaturday(date) {
  // Copy date so don't modify original
  let d = new Date(date);
  // Adjust to previous Saturday
  d.setDate(d.getDate() - (d.getDay() + 1));
  return d;
}

// Samples
[new Date(2019,8,23),
 new Date(2019,8, 1),
 new Date(2019,8, 7),
 new Date(2019,8,12)
].forEach(d => console.log(d.toString()
  + ' => ' +  getLastSaturday(d).toString()
));

Also see get previous saturday's date and next friday's.

Using the built–in parser for unsupported string formats is strongly recommended against, see Why does Date.parse give incorrect results?

The date format "9/7/2019" is ambiguous, it represents 9 July to most people or 7 September to some. The simplest way to avoid confusion is to use the month name rather than number.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • so how would we avoid the ambiguity based on internal date formats? – chovy Jul 01 '22 at 10:21
  • @chovy—what are "internal date formats"? There are 3 formats required to be supported by ECMA-262, anything else is implementation dependent so might return either an expected, unexpected or invalid date. There is some *de facto* standardisation on common US formats, but that should not be relied on. – RobG Jul 01 '22 at 11:11
  • sorry, meant to say "international" not "internal" – chovy Jul 01 '22 at 11:47