46

Does anyone know how can I get next week date based on this week date? example if I have this thursday date (25/6/2009) how can I use javascript to get next thursday date (2/7/2009)?

Jin Yong
  • 42,698
  • 72
  • 141
  • 187

10 Answers10

90
var firstDay = new Date("2009/06/25");
var nextWeek = new Date(firstDay.getTime() + 7 * 24 * 60 * 60 * 1000);

You can also look at DateJS if you like "fluent" APIs.

yummypasta
  • 1,398
  • 2
  • 17
  • 36
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
55
function nextweek(){
    var today = new Date();
    var nextweek = new Date(today.getFullYear(), today.getMonth(), today.getDate()+7);
    return nextweek;
}
glmxndr
  • 45,516
  • 29
  • 93
  • 118
  • Nice - The function is self-documenting and works with edge cases such as leap seconds. – l0b0 Jun 22 '09 at 07:20
  • Nice work. with a slight adaption you can find a day of the week (i.e.: next thursday). ad to the last argument, today.getDate() - today.getDay() + 7 + 4 (that works for thursday) – Lathan Jun 24 '11 at 19:46
  • 5
    This might generate some invalid dates however, which may or may not work, depending on how clever the target JS engines Date() functions are. For instance a week after 30 august 2013 will be 37 august 2013 , which is invalid. – Shayne Aug 30 '13 at 01:12
  • 1
    Check the fiddle that I wrote to see if it works or not on a given browser: http://jsfiddle.net/kymg264r/ – Savas Vedova Dec 09 '14 at 07:54
16

function dateObject.getNextWeekDay returns the next weekday after the object's own date.

Date.prototype.getNextWeekDay = function(d) {
  if (d) {
    var next = this;
    next.setDate(this.getDate() - this.getDay() + 7 + d);
    return next;
  }
}

var now = new Date();
var nextMonday = now.getNextWeekDay(1); // 0 = Sunday, 1 = Monday, ...
var secondNextMonday = new Date(nextMonday).getNextWeekDay(1);
console.log('Next Monday : ' + nextMonday);
console.log('Second Next Monday : ' + secondNextMonday);
Priya
  • 1,522
  • 1
  • 14
  • 31
Volker
  • 497
  • 6
  • 15
9
Date.prototype.addDays = function (d) {
    if (d) {
        var t = this.getTime();
        t = t + (d * 86400000);
        this.setTime(t);
    }
};

this_week.addDays(7);
razzed
  • 2,653
  • 25
  • 27
2

Function that returns next weekdaay:

function getNextWeekDay (startDate, dayOfWeek){
    var dayOffset = dayOfWeek > startDate.getDay()
        ? dayOfWeek - startDate.getDay()
        : dayOfWeek - startDate.getDay() + 7;

    startDate.setDate(startDate.getDate() + dayOffset);

    return startDate;
}

var now = new Date();

var nextMonday = getNextWeekDay(new Date(),1); // 0 = Sunday, 1 = Monday, ...
var nextSaturday = getNextWeekDay(new Date(),6);
var nextSunday = getNextWeekDay(new Date(),0);
var secondNextMonday = getNextWeekDay(new Date(now.getTime() + ( 7 *24 * 60 * 60 * 1000)),1);
alert(nextMonday+ '- '+nextSaturday+ '- '+nextSunday+ '- '+secondNextMonday);
Community
  • 1
  • 1
Nassim Aouragh
  • 333
  • 3
  • 7
1
const getDateAfterWeek = (week) => {
let today = new Date();
  const nextweek = new Date(
    today.getFullYear(),
    today.getMonth(),
    today.getDate() + 7 * week,
  );
  return nextweek;
}
Sultan Aslam
  • 5,600
  • 2
  • 38
  • 44
usman raza
  • 54
  • 6
  • 2
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Aug 04 '20 at 09:42
0

An even more straightforward solution is

let nextWeek = new Date()
nextWeek.setDate(nextWeek.getDate() + 7)

Note that it preserves the current time of day.

David Mold
  • 198
  • 3
  • 5
0

this is my solution.

var today = new Date; // get current date
var first = today.getDate() - today.getDay() + 1 + 7; // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6

var firstday = new Date(today.setDate(first));
var lastday = new Date(today.setDate(last));

console.log(firstday, lastday);
donkey
  • 478
  • 7
  • 10
0

I got it this way:

public nextAnswerDay(answerDay) {
  const now = new Date().getDay();
  let distance = 0

  if (answerDay < now) {
    distance = 7 - (now - answerDay);
  } else {
    distance = Math.abs(now - answerDay);
  }

  let nextAnserDay = new Date();
  nextAnserDay.setDate(nextAnserDay.getDate() + distance);

  return nextAnserDay;
}
Stefano Sansone
  • 2,377
  • 7
  • 20
  • 39
0

There's a fundamental problem with most of the other answers. That is, they don't take into consideration that a different time zone may have a different date, especially if this javascript will be a webpage script.

To deal with this, the date object should be manipulated using the UTC methods. All code should use UTC time to allow the date to return an appropriate local time.

Note: This function returns the upcoming day. Furthermore, it returns today if you're looking for Monday and that's today. (See this answer for the difference between next Monday and the upcoming / this Monday: https://english.stackexchange.com/a/3844)

Without further ado here is the code. As per MDN, the weekday parameter is UTC day 0-6.

        function getUpcomingUTCWeekDay(weekday) {
            let date = new Date();
            let currentDay = date.getUTCDay();
            if (currentDay > weekday) {
                let daysTilNextWeek = 7 - currentDay;
                date.setUTCDate(date.getUTCDate() + daysTilNextWeek);
                currentDay = date.getUTCDay();
            }
            if (currentDay === weekday) {
                return date;
            }
            if (currentDay < weekday) {
                date.setUTCDate(date.getUTCDate() + (weekday - currentDay));
                return date;
            }
            return date;
        }