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

发布于 2024-10-15 05:43:31 字数 210 浏览 2 评论 0原文

我需要网站的 Javascript 代码来自动调整日期。目标是让代码自动将以下语句调整为从现在到永远每个月的第二个星期六:


Next membership meeting: Saturday, MONTH, DAY, YEAR 11 a.m. to noon.


有人有主意吗?非常感谢!

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!

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

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

发布评论

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

评论(3

成熟稳重的好男人 2024-10-22 05:43:31

该函数将为您获取日期对象,您可以从中提取您需要的内容:

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

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));
寄风 2024-10-22 05:43:31

我没有测试,但这是基础知识:

//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;
}

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;
}
我不咬妳我踢妳 2024-10-22 05:43:31

所以,我认为你的问题的核心是:我如何知道每个月的第二个星期六是什么?

未经测试,但这就是我想出的:
它是针对任何一个月的任何第 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. 
    }

第 2 周在该月的 14 天范围内。

我们可以:

首先减去本月开始的星期几的偏移量,

然后第二:

我们可以减去我们要查找的星期几的偏移量。
(这需要是天数的偏移量,因此星期六是 0(零)偏移量。我们从第 n 天的绝对值减去一周中的天数得到这个值。

这给了我们 如果我们在第二个星期六

然后,因为您有一些整数,所以您可以对这些值进行简单的比较,

之前,则显示该日期,如果没有计算下个月的新日期,

希望有所帮助。

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.

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