Function to set a date to a specific day

The following code snippet can be used if you would like to set a date for an upcoming specific day. This example resets the input date to the next Monday. You can make alterations to get other days or days from the previous week.

function setToMonday(d) {
    var day = d.getDay() || 7;
    if (day !== 1)
        d.setDate(d.getDate() + (((1 + 7 - d.getDay()) % 7) || 7));
    return d;
}

Leave a comment

Your email address will not be published. Required fields are marked *