繁体   English   中英

如何在 javascript 中获取下周日期

[英]how to get next week date in javascript

有谁知道如何根据本周日期获得下周日期? 例如,如果我有这个星期四的日期(2009 年 6 月 25 日),我如何使用 javascript 来获取下一个星期四的日期(2009 年 2 月 7 日)?

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

如果您喜欢“流畅”的 API,也可以查看DateJS

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

函数 dateObject.getNextWeekDay 返回对象自己日期之后的下一个工作日。

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

一个更直接的解决方案是

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

请注意,它保留了当前的时间。

这是我的解决方案。

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

我是这样理解的:

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

它更简单,但对我有用;)

大多数其他答案都有一个基本问题。 也就是说,他们没有考虑到不同的时区可能有不同的日期,特别是如果这个 javascript 将是一个网页脚本。

为了解决这个问题,日期 object 应该使用 UTC 方法进行操作。 所有代码都应使用 UTC 时间以允许日期返回适当的本地时间。

注意:此function返回下一天。 此外,如果您正在寻找星期一,它会在今天返回,那就是今天。 (有关下周一和即将到来的/本周一之间的区别,请参阅此答案: https://english.stackexchange.com/a/3844

事不宜迟,这里是代码。 根据MDN ,weekday 参数是 UTC 天 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;
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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