www.webdeveloper.com
Results 1 to 3 of 3

Thread: Script not working for Oct 31st ...

  1. #1
    Join Date
    Apr 2006
    Posts
    19

    Question Script not working for Oct 31st ...

    The below countdown script is not working correctly. It works for most dates but seems to fall over at the 29 of each month. The below script is set for Oct 31 but it displays the 01/Nov ?

    Can anyone please tell me where I'm going wrong with it and/or edit it to make it display the correct date at the end of the month.


    Many Thanks

    <!--

    <script>
    var today=new Date()

    //Enter the occasion's MONTH (1-12) and DAY (1-31):
    var theoccasion=new Date(today.getFullYear(), 10, 31)

    //Customize text to show before and on occasion. Follow grammer below:
    var beforeOccasionText="Left until Halloween!"
    var onOccasiontext="It is Little Spook Day - Happy Halloween!"

    var monthtext=new Array("January","February","March","April","May","June","July","August","September","October","Novem ber","December")
    theoccasion.setMonth(theoccasion.getMonth()-1) //change to 0-11 month format
    var showdate="("+monthtext[theoccasion.getMonth()]+" "+theoccasion.getDate()+")" //show date of occasion

    var one_day=1000*60*60*24
    var calculatediff=""

    calculatediff=Math.ceil((theoccasion.getTime()-today.getTime())/(one_day))
    if (calculatediff<0){ //if bday already passed
    var nextyeartoday=new Date()
    nextyeartoday.setFullYear(today.getFullYear()+1)
    calculatediff=Math.ceil((nextyeartoday.getTime()-today.getTime())/(one_day)+calculatediff)
    }

    //Display message accordingly
    var pluraldayornot=(calculatediff==1)? "Day" : "Days"
    if (calculatediff>0)
    document.write("<center><b>"+calculatediff+" "+pluraldayornot+" "+beforeOccasionText+" "+showdate+"<br><br></b><center>")
    else if (calculatediff==0)
    document.write("<b>"+onOccasiontext+" <br> "+showdate+"!<br><br></b>")

    </script>

    -->

  2. #2
    Join Date
    Sep 2010
    Location
    Melbourne, Australia - on loan from London, UK
    Posts
    56
    Hi

    I'm still trying to figure out WHY... but it seems the days in each month are wrong. The code currently calculates them at:
    jan - 28
    feb - 28
    mar - 30
    apr - 30
    may - 30
    jun - 30
    jul - 31
    aug - 30
    sep - 30
    oct - 30
    nov - 30
    dec - 31

  3. #3
    Join Date
    Sep 2010
    Location
    Melbourne, Australia - on loan from London, UK
    Posts
    56
    Ok I have something... I hope it's the correct answer...

    I went back to the new Date() JS and found that if you code:
    Code:
    new Date(79,5,24)
    ... it produces "Sun Jun 24 1979".
    Notice the month compared to the day. Lol. Yea, days start from 1 whereas months start from 0.

    Going back to your code... it will work if you amend these two lines:
    Code:
    var theoccasion=new Date(today.getFullYear(), 9, 31)
    theoccasion.setMonth(theoccasion.getMonth())
    - Notice I've changed the 10 to a 9 and removed the -1.

Thread Information

Users Browsing this Thread

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

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