Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java, Web
Expertise: Beginner
Aug 31, 1998

Days Left Until Specific Date

Using the Date functionality in JavaScript, you can post the number of days left until a certain date. Internally, JavaScript stores dates as the number of milliseconds from midnight (GMT) on January 1, 1970. If you subtract two Date objects, the result is the number of milliseconds between the two dates. The following code sample subtracts the future (target) date from today's date (returned by Date() with no arguments) and then multiplies to convert milliseconds into days.
 
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">

function datediff(target) {
   // target must in the format
   'Month Day, Year' like 'July 4,
   2000'

   var today = new Date();
   var tomorrow = new Date(target);
   var diff;

   diff = tomorrow - today; // number
 of milliseconds between the two
 dates
   diff = parseInt(diff / (1000 * 60
 * 60 * 24)); // convert to days and
 round off

   document.write(diff);
}

</SCRIPT>
</HEAD>
<BODY>

There are only <SCRIPT
LANGUAGE="JavaScript">datediff('July
4, 2000');</SCRIPT> days left until
liftoff.

</BODY>
</HTML>

Boris Feldman
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date