Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Last Friday of the month 1

Status
Not open for further replies.

NTesla1886

Technical User
Mar 14, 2008
62
US
I have a script on my website that counts down the days until System Admin day. Right now I will have to change it every year.

How can I change the code to determine the date of the last Friday of July and calculate the days until that date?

Kevin
Tektility Inc
Maximizing Potential Through Technology
 
[tt]var y=2008;
var m=6; //meant for july (based 0), reminder: js month based 0;
var d=new Date(y,m+1,1); //first day of next month (month based 0)
var ddiff=(d.getDay()+2)%8; //day difference to subtract
var d_lastfriday=new Date(y,m+1,1-ddiff);
//alert(d+"\n"+d_lastfriday);[/tt]
 
Correction
Sorry! absolutely wrong forumla. This is the correction.
[tt]
var y=2008;
var m=6; //meant for july (based 0), reminder: js month based 0;
var d=new Date(y,m+1,1); //first day of next month (month based 0)
var ddiff=(d.getDay()+[red]1)%7+1[/red]; //day difference to subtract
var d_lastfriday=new Date(y,m+1,1-ddiff);
//alert(d+"\n"+d_lastfriday); [/tt]
 
I have inserted the above code into my script.

Instead of saying that their are 370 or what ever it is until July 31, 2009 it says their are 1216958399974 days.

I have included my script

<Script type="text/javascript" Language = "JavaScipt1.5">
<!--Hide script from old browsers
//This function calculates the number of days until System Administrators Day

days = new Array();
days[0]="Sunday";
days[1]="Monday";
days[2]="Tuesday";
days[3]="Wednesday";
days[4]="Thursday";
days[5]="Friday";
days[6]="Saturday";

months=new Array();
months[0]="January";
months[1]="February";
months[2]="March";
months[3]="April";
months[4]="May";
months[5]="June";
months[6]="July";
months[7]="August";
months[8]="September";
months[9]="October";
months[10]="November";
months[11]="December";

function SysAdmin(Month,Day,Year)
{
var MonthCount=new Array();
MonthCount[1]=31;
MonthCount[2]=28;
MonthCount[3]=31;
MonthCount[4]=30;
MonthCount[5]=31;
MonthCount[6]=30;
MonthCount[7]=31;
MonthCount[8]=31;
MonthCount[9]=30;
MonthCount[10]=31;
MonthCount[11]=30;

if (Year % 4 == 0)
{
MonthCount[2]=29;
}

var MonthTotal=0;

var y=2008;
var m=6; //meant for july (based 0), reminder: js month based 0;
var d=new Date(y,m+1,1); //first day of month
var ddiff=(d.getDay()+1)%7+1; //day difference to subtract
var d_lastfriday=new Date(y,m+1,1-ddiff);
//alert(d+"\n"+d_lastfriday);

for(i=Month;i<7;i++)
{
MonthTotal = MonthTotal + MonthCount;
}
var DayCount=(d_lastfriday - Day) + MonthTotal;
return DayCount;
}
//Stop hiding -->
</Script>

<Script type="text/javascript" Language = "JavaScipt1.5">
<!-- Hide script from old browsers
var ThisWkDayName = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")

//Get date information
var Today = new Date;
var ThisWkDay = Today.getDay();
var ThisDay = Today.getDate();
var ThisMonth = Today.getMonth()+1;
var ThisYear = Today.getYear();

if(ThisYear <= 1999)
{
ThisYear = ThisYear - 100 + 2000;
}

//Get number of days until System Admin Day
var DaysLeft = SysAdmin(ThisMonth,ThisDay,ThisYear);

//Display either the number of days until system Admin Day, or a reminder if the day has arrived
var greeting = "";

if(DaysLeft>0)
{
greeting = "<a href= target=_blank>System Administrators Day</a> is in " + DaysLeft +" days";
}
else
{
greeting = "Thank Your <a href= target=_blank>Systems Admintrator</a> TODAY";
}

// End hiding script from old browsers -->
</script>

Kevin
Tektility Inc
Maximizing Potential Through Technology
 
Just bear in mind that d_lastfriday is a js Date object. Hence to get the Day (dd) in yyyy-mm-dd, you've to go through the getDate() method.

>var DayCount=(d_lastfriday - Day) + MonthTotal;
[tt]var DayCount=(d_lastfriday[red].getDate()[/red] - Day) + MonthTotal;[/tt]

And also I believe there isn't really a need to set up a dictionary on the number of days in each month etc, the difference of Date object is stored essentially in the form of float in unit of millisecond from some reference date. Hence for instance the number of days in the month of February of 2008 is simply given by this.
[tt] n=(new Date(2008,1,1))-(new Date(2008,0,1));
n=n/24/60/60/1000; //add Math.floor(n) if any doubt on resulting non-integer
//alert(n);[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top