使用JavaScript获取给定月份的给定工作日 [英] Get a given weekday in a given month with JavaScript

查看:104
本文介绍了使用JavaScript获取给定月份的给定工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图获得第n个工作日—例如,第二个星期日—某个月的某个日期。

I'm trying to get the nth weekday—for example, the second Sunday—of a month in which a certain date falls.

例如,如果日期是2015年8月24日,我想这样做:

For instance, if the date is August 24, 2015, I'd like to do this:

nthDayOfMonth(0, 2, new Date(2015, 7, 24))

并于2015年8月9日(8月的第2个星期日)获得。然后,我希望能够在日期中添加一个月,再次调用该函数,并在2015年9月13日(9月的第2个星期日)获得。

and get August 9, 2015 (2nd Sunday in August). I'd then like to be able to add a month to the date, call the function again, and get September 13, 2015 (2nd Sunday in September).

以下代码无效的原因。

我缺少什么?

function nthDayOfMonth(day, n, date) {                                                                                              
    console.log(day);
    console.log(n);
    var count = 0; 
    var idate = new Date(date);                                                                                                       

    idate.setDate(1);                                                                                                                 

    while ((count) < n) {                                                                                                             
      idate.setDate(idate.getDate() + 1);
      if (idate.getDay() == day) {
        count++;                                                                                                                      
      }                                                                                                                               
    }                                                                                                                                 

return idate;                                                                                                                     

}

推荐答案

在递增月中的某天之前,您必须检查 idate.getDay()。否则,如果所需的工作日落在该月的第一天,您将得到错误的答案。

You have to check idate.getDay() before incrementing the day of the month. Otherwise you'll get an incorrect answer if the desired weekday falls on the first of the month.

以下代码段演示了更正后的功能。

The following snippet demonstrates the corrected function.

function print(s) {
  document.write(s + '<br />');
}

function nthWeekdayOfMonth(weekday, n, date) {
  var count = 0,
      idate = new Date(date.getFullYear(), date.getMonth(), 1);
  while (true) {
    if (idate.getDay() === weekday) {
      if (++count == n) {
        break;
      }
    }
    idate.setDate(idate.getDate() + 1);
  }
  return idate;
}

             // Second Sunday of the current month.
var date = new Date();
print(date = nthWeekdayOfMonth(0, 2, date)); 
             // Second Sunday of next month.
date.setMonth(date.getMonth() + 1);
print(date = nthWeekdayOfMonth(0, 2, date));

             // First Tuesday of September 2015.
print(nthWeekdayOfMonth(2, 1, new Date(2015, 8)));
             // First Wednesday of September 2015.
print(nthWeekdayOfMonth(3, 1, new Date(2015, 8)));
             // Second Tuesday of September 2015.
print(nthWeekdayOfMonth(2, 2, new Date(2015, 8)));
             // Second Wednesday of September 2015.
print(nthWeekdayOfMonth(3, 2, new Date(2015, 8)));

body {
  font-family: sans-serif;
}

有一种更好的方法可以计算所需的日期而不需要循环。我们首先考虑当月第一天的工作日。假设这是一个星期六,JavaScript调用 6 ,你正在寻找一个星期天, 0

There's an even better approach that calculates the desired date without looping. We start by considering the weekday of the first day of the month. Suppose it's a Saturday, which JavaScript calls 6, and you're looking for a Sunday, which is 0.

要到达当月的第一个星期日,您必须按这个天数提前预订日期:

To get to the first Sunday of the month, you have to advance the date by this number of days:

0 - 6 + 7

结果是1.如何计算有效吗? 0 - 6 是从工作日 6 到工作日 0 ,并将负值变为有效的工作日,我们添加 7

The result is 1. How does the calculation work? 0 - 6 is the number of days from weekday 6 to weekday 0, and to turn a negative value into a valid weekday, we add 7.

一般情况下,从工作日 a 到工作日 b 的天数是

In general, the number of days from weekday a to weekday b is

(b - a + 7) % 7

To继续这个例子,假设我们想要这个月的第一个星期日。在那种情况下,我们到了。但是如果我们想要这个月的第二天,我们必须将日期提前7天。一般来说,给定 n 这样 n == 1 表示给定工作日的第一次出现,我们必须提前通过(n - 1)* 7 天。

To continue the example, suppose that we wanted the first Sunday of the month. In that case, we've arrived. But if we want the second day of the month, we have to advance the date by 7 more days. In general, given n such that n == 1 means the first occurrence of a given weekday, we have to advance by (n - 1) * 7 days.

将所有这些放在一起,如果 date 是这个月的第一天,我们可以到 n 出现工作日通过推进

To put it all together, if date is the first day of the month, we can get to the nth occurrence of weekday by advancing

(weekday - date.getDay() + 7) % 7 + (n - 1) * 7

每月第一天过后的天数。

days past the first day of the month.

此方法在下面实现。

function print(s) {
  document.write(s + '<br />');
}

function nthWeekdayOfMonth(weekday, n, date) {
  var date = new Date(date.getFullYear(), date.getMonth(), 1),
      add = (weekday - date.getDay() + 7) % 7 + (n - 1) * 7;
  date.setDate(1 + add);
  return date;
}

             // Second Sunday of the current month.
var date = new Date();
print(date = nthWeekdayOfMonth(0, 2, date)); 
             // Second Sunday of next month.
date.setMonth(date.getMonth() + 1);
print(date = nthWeekdayOfMonth(0, 2, date));

             // First Tuesday of September 2015.
print(nthWeekdayOfMonth(2, 1, new Date(2015, 8)));
             // First Wednesday of September 2015.
print(nthWeekdayOfMonth(3, 1, new Date(2015, 8)));
             // Second Tuesday of September 2015.
print(nthWeekdayOfMonth(2, 2, new Date(2015, 8)));
             // Second Wednesday of September 2015.
print(nthWeekdayOfMonth(3, 2, new Date(2015, 8)));

body {
  font-family: sans-serif;
}

这篇关于使用JavaScript获取给定月份的给定工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆