2009-10-16 105 views
12

如何返回給定工作日的下一個日期(可以是數字0-6或名稱週日到週六)。在JavaScript中從工作日獲取下一個日期

例如,如果今天,在週五16月 - 2009年我傳入:

  • 週五,它將返回今天的日期16月 - 2009年
  • 週六返回17-Oct-2009
  • 星期四返回22-Oct-2009
+5

這不是租賃編碼器 - 你不應該讓人們爲你做一些工作。請以問題的形式重新修改您的帖子。 – 2009-10-16 16:21:26

+0

我需要一百萬美元。但我不是來這裏要求的。 – 2009-10-16 16:30:22

+0

@Andy E你爲什麼不投票表決這個問題?我對這個社區的用戶感到厭倦和厭倦,只是因爲他們失去了一點而不投降票。如果它在-1,我就不會來到這裏! – 2009-10-16 16:32:04

回答

22

只要加上7並不能解決問題。

下面的函數會給你一週中的第二天。

function nextDay(x){ 
    var now = new Date();  
    now.setDate(now.getDate() + (x+(7-now.getDay())) % 7); 
    return now; 
} 
+2

爲了好奇,這是如何工作的:代碼獲取當月的當天(1-31),並添加今天的星期幾(0-6)與所需星期幾之間的差異(0-6),然後使用mod確保新值不超過6. – Polyducks 2016-05-25 11:14:05

3

要在user 190106answer擴大,此代碼應該給你你想要的東西:

function getNextDay(day, resetTime){ 
    var days = { 
    sunday: 0, monday: 1, tuesday: 2, 
    wednesday: 3, thursday: 4, friday: 5, saturday: 6 
    }; 

    var dayIndex = days[day.toLowerCase()]; 
    if (!dayIndex) { 
    throw new Error('"' + day + '" is not a valid input.'); 
    } 

    var returnDate = new Date(); 
    var returnDay = returnDate.getDay(); 
    if (dayIndex !== returnDay) { 
    returnDate.setDate(returnDate.getDate() + (dayIndex + (7 - returnDay)) % 7); 
    } 

    if (resetTime) { 
    returnDate.setHours(0); 
    returnDate.setMinutes(0); 
    returnDate.setSeconds(0); 
    returnDate.setMilliseconds(0); 
    } 
    return returnDate; 
} 

alert(getNextDay('thursday', true)); 
13

這裏有一個稍作修改的版本蒂姆回答解決具體question--傳遞日期d,和,和上週的希望日期(DOW 0-6),返回日期

function nextDay(d, dow){ 
    d.setDate(d.getDate() + (dow+(7-d.getDay())) % 7); 
    return d; 
} 
+2

作爲一種魅力。小提琴:https://jsfiddle.net/pomqt31x/3/ – Patito 2015-11-27 10:33:43

+0

對於那些需要獲得下一週後一週的一天,或之後的一週:添加一個星期的參數'function nextWeekDay(d,dow,w)'然後將星期添加到計算中並刪除模數:'returnDate.setDate(d.getDate()+(dow +((w * 7)-d.getDay())));' – stackingjasoncooper 2017-11-08 16:04:38

3

這裏是另一個簡單的解決方案

//takes dayIndex from sunday(0) to saturday(6) 
 
function nextDate(dayIndex) { 
 
    var today = new Date(); 
 
    today.setDate(today.getDate() + (dayIndex - 1 - today.getDay() + 7) % 7 + 1); 
 
    return today; 
 
} 
 
document.write("Next Sunday is: "+nextDate(0).toLocaleString()+"<br/>"); 
 
document.write("Next Thursday is: "+nextDate(4).toLocaleString()+"<br/>"); 
 
document.write("Next Saturday is: "+nextDate(6).toLocaleString());

0

如果你不想做通號碼,但平日-名(週日 - 週六)找到某個工作日的將來的日期,那麼這可以幫助您,以及:

function getDateOfWeekday(refday){ 
 
    var days = { 
 
     monday: 1, 
 
     tuesday: 2, 
 
     wednesday: 3, 
 
     thursday: 4, 
 
     friday: 5, 
 
     saturday: 6, 
 
     sunday: 0 
 
    }; 
 
    if(!days.hasOwnProperty(refday))throw new Error(refday+" is not listed in "+JSON.stringify(days)); 
 
    var currDate = new Date(); 
 
    var currTimestamp = currDate.getTime(); 
 
    var triggerDay = days[refday]; 
 
    var dayMillDiff=0; 
 
    var dayInMill = 1000*60*60*24; 
 
    // add a day to dayMillDiff as long as the desired refday (sunday for instance) is not reached 
 
    while(currDate.getDay()!=triggerDay){ 
 
     dayMillDiff += dayInMill; 
 
     currDate = new Date(currDate.getTime()+dayInMill); 
 
    } 
 
    return new Date(currTimestamp + dayMillDiff); 
 
} 
 

 
var sunday = getDateOfWeekday("sunday"); 
 
document.write("Next Sunday is at: <strong>"+sunday.toLocaleString()+"</strong><br/>"); 
 

 
var thursday = getDateOfWeekday("thursday"); 
 
thursday.setHours(0,0,0,0); // set hours/minutes/seconds and millseconds to zero 
 
document.write("Next Thursday is at: <strong>"+thursday.toLocaleString()+"</strong> on midnight<br/>"); 
 

 
var tuesday = getDateOfWeekday("tuesday"); 
 
document.write("Next Tuesday is at: <strong>"+tuesday.toLocaleString()+"</strong><br/>");