开发者

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

I need Javascript code for 开发者_如何学运维a website to automatically adjust a date. 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 a.m. to noon.


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.

    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.

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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜