2009-10-16 143 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/>");