Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Need logic help with date

1. ## 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.

2. 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];

3. ## 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

4. 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; }

5. 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)
}
}

if(theDay <5)secondThurs = 7  + 5 - theDay;
else secondThurs = 9 + theDay;
}

findThur();
</script>```
Eric

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

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

#### Posting Permissions

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