SHARE
Facebook X Pinterest WhatsApp

HTML Goodies: Script Tip: Week 30

Written By
thumbnail
Joe Burns
Joe Burns
Jan 4, 2005

 

Tippinski…

     We will wrap this little one up pretty quickly. It’s all math today. First off, here’s the script once again and it’s effect:





And here’s the script itself.

     In the last script tipwe set two points. The first in right now, and second is January 1, 2000. We also used a small equation to create a day: 1000*60*60*24. Now we have all the parts. Let’s figure the days.

     Here’s the line that does the trick:

daysLeft = (y2k.getTime() – today.getTime()) / (1000*60*60*24);

     We’ve set up a variable, daysLeft, to represent the output of this equation. We take the new date represented by today and the future date represented by y2k, and have the computer get the time. See that? The method “getTime()” is attached to each of the variables. That allows us to now have a set amount of milliseconds between right now and January 1, 2000. The length of time has been figured.

     To get the number of days, we divide the length of time by what is equal to one day. Please notice the format. The author has all of the math on each side of the equation within parentheses so that all of the figuring within will be done first, before the division.

      That’s it. We have our number of days. But as in all math, numbers rarely divide into each other equally. There’s usually a remainder. So we need to allow for that with this:

daysLeft = Math.round(daysLeft);

     This line of code uses the Math object and the “round” method to round off the result of the equation above. The round method will take the result of the number of days and round it to the nearest whole number.

     Notice the format. The variable representing the result of the equation is sitting within the method’s instance. In that position it is being offered to the method as a parameter to be acted upon. Very clever.

     Finally the result is posted to the Web page through a very basic document.write statement:

document.write(“<center><I>~~ Only “+daysLeft+” days until the year 2000 ~~</I></center>”);

     Nothing to it! Just take this script and paste it on your page where you want the result to post. There’s no need for an onLoad trigger. This pup will just roll on by itself. By the way, the result of this script tells you the number of days till January 1, 2000 with today included. If you don’t want today included, the easiest way of doing it is to simply subtract one right before the script displays the results. Just above the document.write statement, add this line:

daysLeft = daysLeft – 1;

     Next week we start a new script. This one is also by request. I guess the running digital clock sparked a few people’s interest in the date. A young man wrote and said he wanted a script that would display the date, but in text form. Something like: “Today is Tuesday, January 15th, 1999”.

     It’s a good suggestion and it’ll allow us to get into arrays and also some fancy coding to get the “th”, “rd”, or “st” following the day’s number. See that above? Cool.

Next Week: A Text Date

    


Learn to write your own JavaScripts with the

Recommended for you...

The Revolutionary ES6 Rest and Spread Operators
Rob Gravelle
Aug 23, 2022
Ahead of Time (AOT) Compilation in Angular
Tariq Siddiqui
Aug 16, 2022
Converting a JavaScript Object to a String
Rob Gravelle
Aug 14, 2022
Understanding Primitive Type Coercion in JavaScript
Rob Gravelle
Jul 28, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.