Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    567
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Need logic help with date

    I need a function that will return the next second Thursday of a month. In other words this month's second thursday has passed so the next second Thursday is Sept 9. I'm not sure how to go about it since I haven't found a function that returns the name of the day.

    Any help would be appreciated.
    Scott Stewart
    Always happy to learn from pros.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Flint, Michigan, USA
    Posts
    632
    Thanks
    1
    Thanked 21 Times in 21 Posts
    To get the name of the day:

    Days=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
    Now=new Date();
    Day=Now.getDay();
    DayName=Days[Day];
    alert(Day+' '+DayName);

  • #3
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts

    Basic Idea

    The logic of the whole process is to look at what the first day of the month is by using getDay() from that you can find the first thursday then you add 7 to that and you can find the second thursday.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Flint, Michigan, USA
    Posts
    632
    Thanks
    1
    Thanked 21 Times in 21 Posts
    Second Thursday of next month:

    Now=new Date();
    Now_M=Now.getMonth();
    Now_Y=Now.getYear();
    if (Now_Y < 70) { Now_Y=Now_Y*1+2000; }
    if (Now_Y < 1900) { Now_Y=Now_Y*1+1900; }
    Then=new Date(Now_Y,Now_M+1,1); // first of next month
    Then_X=Then.getDay();
    Target=12-Then_X;
    if (Target < 8) { Target=Target*1+7; }
    alert(Target);

  • #5
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    They are playing with my development database at work so I had some free time. I think this works, I tested it with a couple of months and it seemed to work correctly.

    Code:
    <script>
      function findThur(){
        var today = new Date();
        var daThur = new Date();
        daThur = getThur(daThur);
        if(today > daThur){
          daThur.setMonth(daThur.getMonth() + 1)
          daThur = getThur(daThur)
        }
        alert(daThur.toLocaleDateString())
      }
    
      function getThur(daDay){
        daDay.setDate(1);
        theDay = daDay.getDay();
        if(theDay <5)secondThurs = 7  + 5 - theDay;
        else secondThurs = 9 + theDay;
        daDay.setDate(secondThurs)
        return daDay
      }
    
      findThur();
    </script>
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #6
    Regular Coder
    Join Date
    Nov 2002
    Posts
    567
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Thanks Alien51!

    Just what I was talking about. I had started top put together the code based on the assumption that a second thursday had to be before the 15th and if the date was before the 15th then go through the dates and see if any thrusdays had occured, then add 7 days to the first thursday found.

    I just used your code. Much cleaner than what I was ending up with.

    Thanks again.
    Scott Stewart
    Always happy to learn from pros.

  • #7
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    I was going to put the check if for the date after X days, but I was too lazy! Yeah, I know one line of code...

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •