簡體   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