2

I am working with a group that meets the second monday of the month and they want their site to reflect the NEXT meeting date. I have the script to show this months second monday, but i am having trouble with the if else statement. I need it to reflect the next upcoming event and not just this months date. IE. this months event date was Aug 13 2012 which is past the current date (aug 21 2012). I would like it to move to the next available date Sept 10 2012. Below is the code i have so far.

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

2 Answers2

1

You're missing () for the x function, so it's not executing it. :) Should be:

if (Date.prototype.x() > todayDate) 

UPDATE: Here is a fixed/working version of the logic cleaned up (and probably overly commented, but I guess it's at least there if anyone needs it).

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 />");
techn1cs
  • 46
  • 3
  • Thank you for catching that! From here what would be the best way for me to change the current date so that i can test it to make sure it works? Its showing up as september 10 2012, but i want to make sure thats not just a false positive. – Andrew Metzger Aug 22 '12 at 02:09
  • I guess I should have fiddled this because it's long? Sorry if so. – techn1cs Aug 22 '12 at 20:42
  • This Is Great! Thank you for the comments, your logic makes a lot of sense! Thank you! I know its a noob question, but using the function you created here how would i write it to the screen? – Andrew Metzger Aug 23 '12 at 01:17
  • You can assign the result to a variable, and then output it however you would like. `var secondMondayDateString = new Date().nextSecondMonday();`, then you could use that with `getElementById` or jQuery or `alert` or what not to show/place the value. (And no worries, you're welcome!) – techn1cs Aug 23 '12 at 04:21
  • I tried this over commented version and works well! – Axel Somerseth Cordova Nov 03 '22 at 22:00
1

If the date of the second Monday of the current month is less than the current date, call the function on the first of the next month.

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
*/
kennebec
  • 102,654
  • 32
  • 106
  • 127