www.webdeveloper.com
Results 1 to 5 of 5

Thread: Help! Displaying welcome message for more complex holidays.

  1. #1
    Join Date
    Feb 2013
    Posts
    1

    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:


    Would anyone be so kind as to point me in the right direction? Thanks!

  2. #2
    Join Date
    Feb 2013
    Posts
    1

    Maybe this will help

    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

  3. #3
    Join Date
    Feb 2006
    Posts
    2,926
    Code:
    <!doctype html>
    <html lang="en">
    <head>
    <meta charset= "utf-8">
    <title>nth days</title>
    
    </head>
    <body>
    <ul>
    <script>
    /*
    You can use an nthofmonth script, where you can call for the 3rd 
    or third Monday of January, 
    for example, for a specified year. 
    This is an example that may give you an approach.
    It returns:
    
    Martin Luther King Day: Monday, January 21, 2013
    Presidents Day: Monday, February 18, 2013
    Arbor Day: Friday, April 26, 2013
    Mothers Day: Sunday, May 12, 2013
    Memorial Day: Monday, May 27, 2013
    Fathers Day: Sunday, June 16, 2013
    Labor Day: Monday, September 02, 2013
    Columbus Day: Monday, October 14, 2013
    Thanksgiving: Thursday, November 28, 2013
    */
    (function(){
        var D2={
            dayShortNames:['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'],
            monthShortNames:['jan', 'feb', 'mar', 'apr', 'may', 'jun',
            'jul', 'aug', 'sep', 'oct', 'nov', 'dec'],
            nthDay: function(ds, y){
                var D, strA, nth, D2= new Date(), wday, m, strA,
                days= Date.dayShortNames,
                months= Date.monthShortNames,
                ords= ['las', 'fir', 'sec', 'thi', 'fou', 'fif'];
                if(!y) y= D2.getFullYear();
                strA= ds.toLowerCase().replace(/\b(in|of|the)\b/g, '');
                strA= strA.replace(/\b([a-z]{3})[a-z]*/g, '$1').trim().split(/\s+/);
                nth= strA[0];
                nth= (parseInt(nth, 10) || ords.indexOf(nth))-1;
                wday= days.indexOf(strA[1]);
                m= months.indexOf(strA[2]);
                if(nth=== -1) m+= 1;
                D2.setFullYear(y, m, 1);
                while(D2.getDay()!= wday) D2.setDate(D2.getDate()+1);
                D2.setDate(D2.getDate()+(7*nth));
                return D2;
            },
            nthHolidays:[
                ['Columbus ', 'second Mon in Oct'],
                ['Arbor ', 'last Friday in April'],
                ['Thanksgiving', '4th Thurs in Nov', 1863],
                ['Labor ', 'first Monday in Sept', 1882],
                ['Memorial ', 'last Mon in May', 1911],
                ['Presidents ', 'third Mon in Feb', 1968],
                ['Mothers ', '2nd Sun in May', 1968],
                ['Fathers ', '3rd Sun in June', 1968],
                ['Martin Luther King ', '3rd Mon in Jan', 1986]
            ]
        }
        for(var p in D2){
            if(D2.hasOwnProperty(p)) Date[p]= D2[p];
        }
    })();
    
    //sample code, y=4 digit year
    (function (y){
        if(!y) y= new Date().getFullYear();
        var list= Date.nthHolidays.map(function(itm){
            if(!itm[2] || y>= itm[2]) return [itm[0], Date.nthDay(itm[1], y)];
        }).sort(function(a, b){
            return a[1]-b[1]
        }).map(function(itm){
            return itm[0].replace(/ $/, ' Day')+': '+itm[1].toLocaleDateString();
        });
        document.write('<li>'+ list.join('</li><li><br>')+'</li>');
    })()
    
    </script>
    </ul>
    
    </body>
    </html>
    Last edited by mrhoo; 02-14-2013 at 09:33 PM.

  4. #4
    Join Date
    Feb 2006
    Posts
    2,926
    I meant to edit the previous post but waited too long- this is a simpler version of the same script-

    <!doctype html>
    <html lang="en">
    <head>
    <meta charset= "utf-8">
    <title>nth days</title>

    </head>
    <body>
    <ul>
    <script>
    Code:
    //IE9< shims:
    if(!Array.prototype.indexOf){
        Array.prototype.indexOf= function(what, i){
            if(!i || typeof i!= 'number') i= 0;
            var L= this.length;
            while(i<L){
                if(this[i]=== what) return i;
                    ++i;
            }
            return -1;
        }
    }
    if(!String.prototype.trim){
            String.prototype.trim= function(){
                return this.replace(/^\s+/, '').replace(/\s+$/, '');
            }
    
    }
    (function(){
        var D2={
            dayShortNames:['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'],
            monthShortNames:['jan', 'feb', 'mar', 'apr', 'may', 'jun',
            'jul', 'aug', 'sep', 'oct', 'nov', 'dec'],
            nthDay: function(ds, y){
                var D, strA, nth, D2= new Date(), wday, m, strA,
                days= Date.dayShortNames,
                months= Date.monthShortNames,
                ords= ['las', 'fir', 'sec', 'thi', 'fou', 'fif'];
                if(!y) y= D2.getFullYear();
                strA= ds.toLowerCase().replace(/\b(in|of|the)\b/g, '');
                strA= strA.replace(/\b([a-z]{3})[a-z]*/g, '$1').split(/\s+/);
                nth= strA[0];
                nth= (parseInt(nth, 10) || ords.indexOf(nth))-1;
                wday= days.indexOf(strA[1]);
                m= months.indexOf(strA[2]);
                if(nth=== -1) m+= 1;
                D2.setFullYear(y, m, 1);
                while(D2.getDay()!= wday) D2.setDate(D2.getDate()+1);
                D2.setDate(D2.getDate()+(7*nth));
                return D2;
            }
        }
        for(var p in D2){
            if(D2.hasOwnProperty(p)) Date[p]= D2[p];
        }
    })();
    
    //sample code, y=4 digit year
    (function(y){
        if(!y) y= new Date().getFullYear();
        var list= ['Martin Luther King Day: '+
        Date.nthDay('3rd Mon in Jan', 2013).toLocaleDateString(),
        'Presidents Day: '+
        Date.nthDay('third Mon in Feb', 2013).toLocaleDateString(),
        'Arbor Day: '+
        Date.nthDay('last Friday in April', 2013).toLocaleDateString(),
        'Mothers Day: '+
        Date.nthDay('2nd Sun in May', 2013).toLocaleDateString(),
        'Memorial Day: '+
        Date.nthDay('last Mon in May', 2013).toLocaleDateString(),
        'Fathers Day: '+
        Date.nthDay('3rd Sun in June', 2013).toLocaleDateString(),
        'Labor Day: '+
        Date.nthDay('first Monday in Sept', 2013).toLocaleDateString(),
        'Columbus Day: '+
        Date.nthDay('second Mon in Oct', 2013).toLocaleDateString(),
        'Thanksgiving: '+
        Date.nthDay('4th Thurs in Nov', 2013).toLocaleDateString()
        ];
        document.write('<li>'+ list.join('</li><li><br>')+'</li>');
    })();
    </script>
    </ul>
    </body>
    </html>
    Last edited by mrhoo; 02-14-2013 at 11:04 PM. Reason: formatting

  5. #5
    Join Date
    Sep 2007
    Posts
    301
    Code:
      
    <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.
    İyiliği emret. Kötülükten alıkoy. (31/16)

    hasbunallahuVeNimelVekil++

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles