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 4 of 4
  1. #1
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    919
    Thanks
    76
    Thanked 29 Times in 29 Posts

    Comparing 2 different date objects to retrieve the difference

    Hi, I have a site which I want to add some javascript date functionality to.

    It's just a mock up local business site, something I'm practicing on. I am putting an 'Exclusive Offer' section to my homepage where I display some products and then there is a line below the offers which says:

    "YOU HAVE 40 days left" for example.

    The date I want the offer to end is 25th March 2013.

    What I thought to do was create 2 date objects. The first, get the local computer date, and set the other date object to 25 March 2013. I wanted to compare the milliseconds of these dates, and then convert it into how many days are between the both of them.

    I can't even seem to retrieve the milliseconds from either date object since 1970 Jan 00:00.

    Here is what I've done so far:
    Code:
    <html>
    <head>
       <title>Vs Page</title>
    </head>
    
    <body>
    
    <script type="text/javascript">
    
    var todays_date = new Date();
    document.write(todays_date+"<br />");
    
    var seconds = todays_date.getUTCMilliseconds();
    document.write(seconds+"<br />");
    
    var offer_end_date = new Date("25 March 2013");
    document.write(offer_end_date+"<br />");
    
    document.write();
    </script>
    
    </body>
    </html>
    The method getUTCMilliseconds(); just returns a number between 0-999 but I need all of the milliseconds since Jan 1970 00:00.

    Not sure if I am thinking into this too much or if this is even the correct way to do this.

    Thank you for any help, links, tips guidance.

    Kind regards,

    LC.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,895
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    You need getTime(), which returns the number of milliseconds since midnight UTC Jan 1, 1970, not getUTCMilliseconds.()


    Code:
    <body onload = "showdaysleft()">
    
    <span id = "offerends"></span>
    
    <script type = "text/javascript">
    
    function showdaysleft() {
    var now = new Date().getTime();
    var offerdate = new Date(2013,2,25).getTime();
    var diff = offerdate - now;
    if (diff > 0) {
    daysdiff = Math.floor(diff / (24*60*60*1000));  // milliseconds in a day
    document.getElementById("offerends").innerHTML = "YOU HAVE " + daysdiff + " DAYS LEFT";
    }
    
    }
    </script>
    document.write() is in effect obsolete. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.


    Mother Theresa was beautified ten years ago today. - BBC Radio Scotland
    Last edited by Philip M; 11-02-2012 at 03:33 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    919
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Ah thank you very much indeed. I was only using document.write() in this newly created page just to get the values so I could see them. I was going to paste the end result (which is the days) into the actual site once I had the functionality.

    I spent a whole day once trying to figure out why my original content was being overwritten due to the very same issue you just told me about

    I was not best pleased to say the least when I realized I should have been using innerHTML

    P.S - Please could you just explain the equation in the floor method, the (24*60*60*1000)? Is that the same as saying, 24 hours in a day, 60 minutes in an hour, 60 seconds in a minute and 1000 milliseconds in a second? All that multiplied == the milliseconds in a day?

    Kind regards!

    LC.
    Last edited by LearningCoder; 11-02-2012 at 06:09 PM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,895
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Quote Originally Posted by LearningCoder View Post
    P.S - Please could you just explain the equation in the floor method, the (24*60*60*1000)? Is that the same as saying, 24 hours in a day, 60 minutes in an hour, 60 seconds in a minute and 1000 milliseconds in a second? All that multiplied == the milliseconds in a day?
    Yes, that's right.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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