S
S
Sergey2018-12-13 10:09:09
JavaScript
Sergey, 2018-12-13 10:09:09

How to get the date of the next Saturday?

How to get the date of the nearest future Saturday? That is, for example, the user logged in today and I need to determine that the date is the next Saturday 15.12.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
0
0xD34F, 2018-12-13
@1Sergey1

function getDateOfNearestDay(day, date = new Date()) {
  return new Date(date.getFullYear(), date.getMonth(), date.getDate() + ((7 + day - date.getDay()) % 7));
}

const dateOfNearestSaturday = getDateOfNearestDay(6);

I
Ihor Bratukh, 2018-12-13
@BRAGA96

function getSaturday() {
    var today = new Date().getUTCDay();
    var diff = 6 - today;
    if ([-1, 0].includes(Math.sign(diff))) {
        return new Date(new Date().setUTCDate(13 - today));
    }
    return new Date(new Date().setUTCDate(diff));
}

D
Daniel, 2018-12-13
@DarkTemplar_0

const getSaturday = () => {
  let daysUntil = 6 - new Date().getDay(),
       date = new Date().toLocaleDateString().split("-");

  date[2] = parseInt(date[2]) + daysUntil;
  
  return date.join("-"); 		
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question