在 JavaScript 中获取工作日的下一个日期

发布于 2024-08-08 14:27:15 字数 333 浏览 7 评论 0原文

如何返回给定工作日的下一个日期(可以是数字 0-6 或名称 Sunday-Saturday)。

例如,如果今天,2009 年 10 月 16 日,我传入:

  • 星期五,它将返回今天的日期 2009 年 10 月 16 日
  • < strong>星期六返回2009年10月17日
  • 星期四返回2009年10月22日

How can one return the next date of a given weekday (it could be either a number 0-6 or names Sunday-Saturday).

Example, if today, on Friday 16-Oct-2009 I passed in:

  • Friday, it would return today's date 16-Oct-2009
  • Saturday returns 17-Oct-2009
  • Thursday returns 22-Oct-2009

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

笔落惊风雨 2024-08-15 14:27:16

仅仅添加 7 并不能解决问题。

下面的函数将为您提供一周中的第二天。

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

Just adding 7 doesn't solve the problem.

The below function will give you the next day of the week.

function nextDay(x){
    var now = new Date();    
    now.setDate(now.getDate() + (x+(7-now.getDay())) % 7);
    return now;
}
另类 2024-08-15 14:27:16

这是对 Tim 的答案稍加修改的版本,以解决特定问题 - 传递日期 d,以及所需的一周中的某一天(dow 0-6),返回日期

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

Here's a slightly modified version to Tim's answer to address the specific question-- pass in a date d, and, and a desired day of week (dow 0-6), return the date

function nextDay(d, dow){
    d.setDate(d.getDate() + (dow+(7-d.getDay())) % 7);
    return d;
}
方圜几里 2024-08-15 14:27:16

这是另一个简单的解决方案

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

Here is another simple solution

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

橘香 2024-08-15 14:27:16

如果您不想通过数字而是通过工作日名称(星期日 - 星期六)来查找某个工作日的未来日期,那么这也可以帮助您:

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

And if you do not want do pass numbers but weekday-names (sunday - saturday) to find a future date of a certain weekday, then this helps you as well:

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

渔村楼浪 2024-08-15 14:27:16

展开用户 190106答案,此代码应该为您提供您想要的内容:

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 !== undefined) {
    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));

To expand on user 190106's answer, this code should give you what you wanted:

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 !== undefined) {
    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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文