简体   繁体   English

使用Javascript自动将日期调整为每月的第2个星期六?

[英]Using Javascript to automatically adjust date to 2nd Saturday of every month?

I need Javascript code for a website to automatically adjust a date. 我需要网站的Javascript代码才能自动调整日期。 The goal is to have the code automatically adjust the following statement to be the second Saturday of every month from now until eternity: 目标是使代码自动将以下语句调整为从现在起到永恒的每个月的第二个星期六:


Next membership meeting: Saturday, MONTH, DAY, YEAR 11 am to noon. 下一次会员会议: YEARMONTH,DAY,DAY,上午11点至中午。


Anyone have an idea? 有人有主意吗? Much appreciated! 非常感激!

This function will get you the date object, you can pull out what you need from it: 此函数将为您提供日期对象,您可以从中提取所需的内容:

var getMeeting = function(year, month){
    var date = new Date(year, month, 1, 0, 0, 0, 0);
    date.setDate(14-date.getDay());
    return date;
};


alert(getMeeting(2011,5));

I didn't test but here is the basics: 我没有测试,但是这里是基础知识:

//our main code
var Months = ["Jan", "Feb", "Mar", /*... you finish... */ ];
var meetingDate = getMonthlyMeeting();
document.Write( "<i>Next membership meeting:</i> Saturday, " + Months[meetingDate.getMonth()] + ", " + meetingDate.getDay() + ", " + meetingDate.getYear() + " 11 a.m. to noon.");



// call this to get the monthly meeting date
// returns a Date() object
function getMonthlyMeeting(){
var today = new Date(); //JS automatically initializes new Date()s to the current time

//first, see if today is our meeting day
var meetingDate;
var thisMonthsMeeting = getSecondTuesdayInMonth(today.getMonth(), today.getYear());
if( thisMonthsMeeting.getDay() == today.getDay() ){
  // today is our meeting day!
  meetingDate = today;
}
else {
 if  ( today.getDay() < thisMonthsMeeting.getDay() ){
   // it hasn't happened this month yet
   meetingDate = thisMonthsMeeting;
 } else {
   //this month's meeting day has already passed
   if( today.getMonth() == 11 ){
        // rolling over to the next year
        meetingDate = getSecondTuesdayInMonth(0, today.getYear() + 1);
   } else {
        meetingDate = getSecondTuesdayInMonth(today.getMonth() + 1, today.getYear());
   }
 }
}
return meetingDate;
}

// this is a helper function to get the second tuesday in any month
// returns a Date() object
function getSecondTuesdayInMonth(var month, var year){
var saturdays = 0;
var testDay= new Date();
while( testDay.getDay() != 2 && saturdays < 2 ){
  //while the day we are testing isnt tuesday (2) and we haven't found it twice
  if( testDay.getDay() == 2 )
    saturdays = saturdays + 1; //we found a saturday
  testDay= new Date(testDay.getTime() + 86400000); //increment our day to the next day
}
//when we finish the while loop, we are on our day
return testDay;
}

So, I figure that the meat of your problem is: How do I know what the second saturday of each month is? 因此,我认为您遇到的问题是:我怎么知道每个月的第二个星期六?

Not tested, but this is what I came up with: It is abstracted for any nth day of any month. 没有经过测试,但这是我想出的:它在每月的第n天被抽象。

    nthDate = function(nth_week, nth_day, month){
        var src_date = new Date();

        src_date.setDate(1);
        src_date.setMonth(month);

        return ( (nth_week * 7) - src_date.getDay() ) - ( Math.abs( nth_day - 6) );
    };

    var cur_date = new Date();

    var cur_day = cur_date.getDay();

    //2 for the 2nd week of the month
    //6 is the integer value for saturday (days of the week 0-6)
    var nth_date = nthDate( 2, 6, cur_date.getMonth() );

    if(cur_day < nth_date){
       //display the upcoming date here
    }else if( cur_day  > nth_date){
       //figure out next month's date and display that
       var next_date = nthDate(2, 6, (cur_date.getMonth() +1) );
       //does this deal with the case of the month being december?? not sure. 
    }

The 2nd week is in the range of 14 days into the month. 第二周为每月14天。

We can: 我们可以:

first subtract the offset for the day of the week that this month starts with, 首先减去该月开始的星期几的偏移量,

then second: 然后第二:

we can subtract the offset for the day of the week that we are looking for. 我们可以减去要查找的星期几的偏移量。 (this needs to be the offset of days , so saturday is a 0 (zero) offset. We get this value from the absolute value of nth day minus the number of days in the week. (这需要是days偏移量 ,因此saturday是0(零)偏移量。我们从第n天的绝对值减去一周中的天数获得此值。

This gives us the date of the second saturday. 这给了我们第二个星期六的日期。

Then, because you have some ints you can do a simple compare against the values. 然后,由于您有一些整数,因此可以对这些值进行简单的比较。

If we're before the second saturday, display that, if not calculate a new date for next month. 如果我们在第二个星期六之前,请显示该日期,如果不计算下个月的新日期。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM