使用Javascript自动将日期调整到每月的第二个星期六?
我需要一个网站的Javascript代码来自动调整日期。目标是让代码自动将以下语句调整为从现在到永远的每月第二个星期六:使用Javascript自动将日期调整到每月的第二个星期六?,javascript,object,date,Javascript,Object,Date,我需要一个网站的Javascript代码来自动调整日期。目标是让代码自动将以下语句调整为从现在到永远的每月第二个星期六: 下次会员会议:月、日、年、星期六上午11时至中午 有人有主意吗?非常感谢 我没有进行测试,但以下是基本内容: //our main code var Months = ["Jan", "Feb", "Mar", /*... you finish... */ ]; var meetingDate = getMonthlyMeeting(); document.Write(
下次会员会议:月、日、年、星期六上午11时至中午
有人有主意吗?非常感谢 我没有进行测试,但以下是基本内容:
//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;
}
//我们的主代码
var月数=[“1月”、“2月”、“3月”/*…您完成…*/];
var meetingDate=getMonthlyMeeting();
文档。编写(“下一次会员会议:星期六,“+Months[meetingDate.getMonth()]+”,“+meetingDate.getDay()+”,“+meetingDate.getYear()+”,“+meetingDate.getYear()+”,“上午11点到中午。”);
//拨打此电话以获取每月会议日期
//返回日期()对象
函数getMonthlyMeeting(){
var today=new Date();//JS自动将新日期()初始化为当前时间
//首先,看看今天是不是我们的会面日
var会议日期;
var thisMonthsMeeting=getSecondTuesdayInMonth(今天.getMonth(),今天.getYear());
如果(ThisMonthMeeting.getDay()==今天.getDay()){
//今天是我们的会面日!
会议日期=今天;
}
否则{
如果(今天.getDay()
我没有进行测试,但以下是基本内容:
//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;
}
//我们的主代码
var月数=[“1月”、“2月”、“3月”/*…您完成…*/];
var meetingDate=getMonthlyMeeting();
文档。编写(“下一次会员会议:星期六,“+Months[meetingDate.getMonth()]+”,“+meetingDate.getDay()+”,“+meetingDate.getYear()+”,“+meetingDate.getYear()+”,“上午11点到中午。”);
//拨打此电话以获取每月会议日期
//返回日期()对象
函数getMonthlyMeeting(){
var today=new Date();//JS自动将新日期()初始化为当前时间
//首先,看看今天是不是我们的会面日
var会议日期;
var thisMonthsMeeting=getSecondTuesdayInMonth(今天.getMonth(),今天.getYear());
如果(ThisMonthMeeting.getDay()==今天.getDay()){
//今天是我们的会面日!
会议日期=今天;
}
否则{
如果(今天.getDay()
所以,我想你的问题的核心是:我怎么知道每个月的第二个星期六是什么
未经测试,但我得出以下结论:
它是为任何月份的任何第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.
}
nthDate=函数(第n周、第n天、月){
var src_date=新日期();
src_date.setDate(1);
src_日期设置月(月);
return((第n周*7)-src_date.getDay())-(Math.abs(第n周*6));
};
var cur_date=新日期();
var cur_day=cur_date.getDay();
//本月第2周的2
//6是星期六的整数值(一周中的几天0-6)
var nth_date=nthDate(2,6,cur_date.getMonth());
如果(当前日期<第n个日期){
//在此处显示即将到来的日期
}否则如果(当前日期>第n个日期){
//算出下个月的日期并显示出来
var next_date=nthDate(2,6,(cur_date.getMonth()+1));
//这个月是十二月吗?不确定。
}
第二周在该月的14天范围内
我们可以:
首先减去本月开始的星期几的偏移量
其次:
我们可以减去我们正在寻找的一周中某一天的偏移量。
(这需要是天的偏移量,所以星期六是0(零)偏移量。我们从第n天的绝对值减去一周中的天数得到这个值
这给了我们第二个星期六的日期
然后,因为您有一些整数,所以可以对这些值进行简单的比较
如果我们在第二个星期六之前,显示它,如果不计算下个月的新日期
希望这能有所帮助。所以,我想你的主要问题是:我怎么知道每个月的第二个星期六是什么 未经测试,但我得出以下结论: 它是为任何月份的任何第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.
}