当月第二个星期一的Javascript日期

当月第二个星期一的Javascript日期,javascript,date,Javascript,Date,我正在与一个在本月第二个星期一开会的小组合作,他们希望他们的网站反映下次会议的日期。我有这个月的第二个星期一的脚本,但是我在if-else语句中遇到了问题。我需要它来反映下一个即将到来的事件,而不仅仅是这个月的日期。即,本月事件日期为2012年8月13日,超过当前日期2012年8月21日。我希望它移到下一个可用日期2012年9月10日。下面是我到目前为止的代码 <script type="text/javascript"> Date.prototype.x = function ()

我正在与一个在本月第二个星期一开会的小组合作,他们希望他们的网站反映下次会议的日期。我有这个月的第二个星期一的脚本,但是我在if-else语句中遇到了问题。我需要它来反映下一个即将到来的事件,而不仅仅是这个月的日期。即,本月事件日期为2012年8月13日,超过当前日期2012年8月21日。我希望它移到下一个可用日期2012年9月10日。下面是我到目前为止的代码

<script type="text/javascript">
Date.prototype.x = function () {
var d = new Date (this.getFullYear(), this.getMonth(), 1, 0, 0, 0)
d.setDate (d.getDate() + 15 - d.getDay())
return d
}
Date.prototype.getSecondMonday = function () {
var d = new Date (this.getFullYear(), 1, 1, 0, 0, 0)
d.setMonth(this.getMonth()+1)
d.setDate (d.getDate() + 15 - d.getDay())
return d
}
var today = new Date()
var todayDate = today.toDateString()

if (Date.prototype.x>todayDate)
  {
document.write (new Date().x().toDateString());
  }
else
  {
document.write (new Date().getSecondMonday().toDateString());
  }
</script>
您缺少x函数,因此它没有执行它:应该是:

if (Date.prototype.x() > todayDate) 
更新: 这里有一个固定/工作版本的逻辑清理和可能过度评论,但我想它至少在那里,如果有人需要它

Date.prototype.nextSecondMonday = function (){
    // Load the month.
    var target = new Date(this.getFullYear(), this.getMonth(), 1, 0, 0, 0);
    var today = new Date();

    // Check to see if the 1st is on a Monday.
    var isMonday = (target.getDay() == 1);

    // Jump ahead two weeks from the 1st, and move back the appropriate number of days to reach the preceding Monday.
    // i.e. If the 1st is a Thursday, we would move back three days.
    var targetDate = 15 - (target.getDay() - 1);

    // Quick adjustment if the 1st is a Monday.
    if (isMonday) targetDate -= 7;

    // Move to the second Monday in the month.   
    target.setDate(targetDate);

    // Second Monday is before today's date, so find the second Monday next month.
    if (today > target) {
        //return "<em>" + target.toLocaleDateString() + " is in the past...</em>";
        target.setMonth(target.getMonth() + 1);
        return target.nextSecondMonday();
    }

    // Format and return string date of second Monday.
    return target.toLocaleDateString();
}


// Working test for the year 2012.
//for (var i = 0; i < 12; i++)
    //$("#log").append(new Date(2012, i).nextSecondMonday() + "<br /><br />");
您缺少x函数,因此它没有执行它:应该是:

if (Date.prototype.x() > todayDate) 
更新: 这里有一个固定/工作版本的逻辑清理和可能过度评论,但我想它至少在那里,如果有人需要它

Date.prototype.nextSecondMonday = function (){
    // Load the month.
    var target = new Date(this.getFullYear(), this.getMonth(), 1, 0, 0, 0);
    var today = new Date();

    // Check to see if the 1st is on a Monday.
    var isMonday = (target.getDay() == 1);

    // Jump ahead two weeks from the 1st, and move back the appropriate number of days to reach the preceding Monday.
    // i.e. If the 1st is a Thursday, we would move back three days.
    var targetDate = 15 - (target.getDay() - 1);

    // Quick adjustment if the 1st is a Monday.
    if (isMonday) targetDate -= 7;

    // Move to the second Monday in the month.   
    target.setDate(targetDate);

    // Second Monday is before today's date, so find the second Monday next month.
    if (today > target) {
        //return "<em>" + target.toLocaleDateString() + " is in the past...</em>";
        target.setMonth(target.getMonth() + 1);
        return target.nextSecondMonday();
    }

    // Format and return string date of second Monday.
    return target.toLocaleDateString();
}


// Working test for the year 2012.
//for (var i = 0; i < 12; i++)
    //$("#log").append(new Date(2012, i).nextSecondMonday() + "<br /><br />");

如果当月第二个星期一的日期小于当前日期, 在下个月的第一天调用该函数

Date.prototype.nextSecondMonday= function(){
    var temp= new Date(this), d= temp.getDate(), n= 1;
    while(temp.getDay()!= 1) temp.setDate(++n);
    temp.setDate(n+7);
    if(d>temp.getDate()){
        temp.setMonth(temp.getMonth()+1, 1);
        return temp.nextSecondMonday();
    }
    return temp.toLocaleDateString();
}


/*  tests


var x= new Date(2012, 7, 22);
x.nextSecondMonday()
Monday, September 10, 2012

var x= new Date(2012, 7, 12);
x.nextSecondMonday()
Monday, August 13, 2012
*/

如果当月第二个星期一的日期小于当前日期, 在下个月的第一天调用该函数

Date.prototype.nextSecondMonday= function(){
    var temp= new Date(this), d= temp.getDate(), n= 1;
    while(temp.getDay()!= 1) temp.setDate(++n);
    temp.setDate(n+7);
    if(d>temp.getDate()){
        temp.setMonth(temp.getMonth()+1, 1);
        return temp.nextSecondMonday();
    }
    return temp.toLocaleDateString();
}


/*  tests


var x= new Date(2012, 7, 22);
x.nextSecondMonday()
Monday, September 10, 2012

var x= new Date(2012, 7, 12);
x.nextSecondMonday()
Monday, August 13, 2012
*/

谢谢你抓住了!从这里开始,对我来说更改当前日期的最佳方式是什么,这样我就可以测试它以确保它工作正常?它显示为2012年9月10日,但我想确保这不仅仅是一个假阳性。我想我应该把它弄乱,因为它很长?如果是的话,很抱歉。这太棒了!谢谢你的评论,你的逻辑很有道理!非常感谢。我知道这是一个noob问题,但是使用您在这里创建的函数,我将如何将其写入屏幕?您可以将结果分配给一个变量,然后根据需要输出它。var secondMondayDateString=新日期。nextSecondMonday;,然后,您可以将其与getElementById、jQuery、alert或其他不显示/放置该值的内容一起使用。别担心,不客气!谢谢你抓住了!从这里开始,对我来说更改当前日期的最佳方式是什么,这样我就可以测试它以确保它工作正常?它显示为2012年9月10日,但我想确保这不仅仅是一个假阳性。我想我应该把它弄乱,因为它很长?如果是的话,很抱歉。这太棒了!谢谢你的评论,你的逻辑很有道理!非常感谢。我知道这是一个noob问题,但是使用您在这里创建的函数,我将如何将其写入屏幕?您可以将结果分配给一个变量,然后根据需要输出它。var secondMondayDateString=新日期。nextSecondMonday;,然后,您可以将其与getElementById、jQuery、alert或其他不显示/放置该值的内容一起使用。别担心,不客气!