Help! Displaying welcome message for more complex holidays.
Hello all.
I am attempting to create a welcome message for my company's Spiceworks user portal which displays a message based on the date (used for holidays or other important dates). I have a script which does just that, but only for static dates (December 25, July 4, September 11). I would like to know how to do the same for the more complex holidays (Thanksgiving is the fourth Thursday in November, for example), but I can't figure out how to do so. Here is the code which I use currently to point out important dates:
Hi,
so you will need to find out that what day of that particular year is in November.
for example in Firefox you can get this by;
var y = today.getFullYear();
$first_day = new Date(y,10,1);
to_s = $first_day.toString();
to_s.substring(0,3);
this will give :
From full year
Date {Fri Nov 01 2013 00:00:00 GMT-0400 (EDT)}
to string
"Fri Nov 01 2013 00:00:00 GMT-0400 (EDT)"
get the first 3 letter
"Fri"
then using switch statement to find the date that will fall into the 4th week on Thursday. And get the exact date of Thanksgiving. Then compare with the current day ( get current day in javascript) with this, and if it is, then display the message. This if statement can be check if the getMonth falls in November, so you don't have to check every time the script runs.
I learned how to do this on PHP and I am new to javaScript. But I think you can do it. please let me know how you do with this project.
thanks
<script type="text/javascript">
// mayıs ayının 2. pazar günü
Date.mothersDay=function( y ){
var d = new Date(y,4,1);
var n = d.getDay();
d.setDate( ( ( 7 - d.getDay() ) % 7 )+ 8 );
return d;
}
alert(Date.mothersDay(2016));
// 2016 da ikiinci pazar ayın 8 i oluyor.
alert(Date.mothersDay(2013));
alert(Date.mothersDay(2015) );
// haziran ayının 3. pazar günü
Date.fathersDay=function( y ) {
var d = new Date(y,5,1);
d.setDate( ( ( 7 - d.getDay() ) % 7 )+ 15 );
return d;
}
alert(Date.fathersDay(2016));
alert(Date.fathersDay(2012));
alert(Date.fathersDay(2014));
alert(Date.fathersDay(2013));
</script>
Last edited by Ayşe; 02-19-2013 at 04:01 PM.
The Time Through Ages
In the Name of Allah, Most Gracious, Most Merciful
1. By the Time,
2. Verily Man is in loss,
3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.
Bookmarks