简体   繁体   中英

how to get next week date in javascript

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)?

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.

function nextweek(){
    var today = new Date();
    var nextweek = new Date(today.getFullYear(), today.getMonth(), today.getDate()+7);
    return nextweek;
}

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);

Date.prototype.addDays = function (d) {
    if (d) {
        var t = this.getTime();
        t = t + (d * 86400000);
        this.setTime(t);
    }
};

this_week.addDays(7);

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);
const getDateAfterWeek = (week) => {
let today = new Date();
  const nextweek = new Date(
    today.getFullYear(),
    today.getMonth(),
    today.getDate() + 7 * week,
  );
  return nextweek;
}

An even more straightforward solution is

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

Note that it preserves the current time of day.

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);

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;
}

Its simpler, but worked for me ;)

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.setDate(date.getUTCDate() + daysTilNextWeek);
                currentDay = date.getUTCDay();
            }
            if (currentDay === weekday) {
                return date;
            }
            if (currentDay < weekday) {
                date.setDate(date.getUTCDate() + (weekday - currentDay));
                return date;
            }
            return date;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM