Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    43
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Lightbulb Days Counter Until a date

    Here is an javascript that counts the days until a date.
    I'm using as my Kaspersky Antivirus 2013 License counter.

    Code:
    <BODY>
    <SCRIPT LANGUAGE="JavaScript">
    var date = new Date("December 1, 2013");
    var description = " Kaspersky Antivirus 2013 expire";
    var now = new Date();
    var diff = date.getTime() - now.getTime();
    var days = Math.floor(diff / (1000 * 60 * 60 * 24));
    document.write("<center><h3>")
    if (days > 1) {
    document.write(days+1 + " days until this" + description);
    }
    else if (days == 1) {
    document.write("Only two days until " + description);
    }
    else if (days == 0) {
    document.write("Tomorrow " + description);
    }
    else {
    document.write(" " + description + "d!");
    }
    document.write("</h3></center>");
    </script>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    Well, yes and no.

    You are using document.write( ) which is really to be avoided, if possible.

    And the <center> tag is considered obsolete in modern HTML, certainly obsolescent.

    And it would also be better to avoid using any global variables, to ensure you won't have an collisions with other possible JavaScript on the page.

    So...

    Better would be perhaps something like this:
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <div id="expires" style="font-size: x-large; font-weight: bold; width: 100%; text-align: center;"></div>
    ... other content on page ...
    <script type="text/javascript">
    (
      function( )
      {
          var diff = (new Date(2013,11,1)).getTime() - (new Date()).getTime();
          var days = Math.floor( diff / (24 * 60 *60 * 1000) );
          var left = ( days > 1 ) ? (days + " days left until")
                                  : ( days == 1 ) ? "Only two days until" 
                                  : ( days == 0 ) ? "Tomorrow"
                                  : "";
          document.getElementById("expires").appendChild(
              document.createTextNode( left + " Kaspersky Antivirus 2013 expires")
          );
      }
    )();
    </script>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Oct 2008
    Posts
    43
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Thumbs up

    Yes, good rewrite will use it. Just missed 2 things, the number of days isn't the same even changing the typo new Date(2013,11,1)) and nothing happens when the counter has pass 1/12/2013.

    Good Night

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    Wrong.

    new Date( 2013, 11, 1 ) *IS* December 1, 2013.

    In JavaScript, month numbers go from 0 to 11, *NOT* from 1 to 12.

    Not sure what you mean by "nothing happens". It does the exact same thing your code does: Displays only "Kaspersky Antivirus 2013 expires" with no prefix.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Oct 2008
    Posts
    43
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Thanks, i didn't know that about the javascript dates.


  •  

    Posting Permissions

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