View Full Version : Need logic help with date

ScottInTexas

08-30-2004, 02:11 PM

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.

jalarie

08-30-2004, 04:14 PM

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

A1ien51

08-30-2004, 04:22 PM

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

jalarie

08-30-2004, 04:22 PM

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

A1ien51

08-30-2004, 04:55 PM

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.

<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

ScottInTexas

08-30-2004, 08:19 PM

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.

A1ien51

08-30-2004, 08:31 PM

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

Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.