...

View Full Version : Comparing 2 different date objects to retrieve the difference



LearningCoder
11-02-2012, 03:04 PM
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:


<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.:)

Philip M
11-02-2012, 03:26 PM
You need getTime(), which returns the number of milliseconds since midnight UTC Jan 1, 1970, not getUTCMilliseconds.()



<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

LearningCoder
11-02-2012, 06:06 PM
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 :o

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

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.:thumbsup:

Philip M
11-02-2012, 06:40 PM
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. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum