SHARE
Facebook X Pinterest WhatsApp

HTML Goodies: Script Tip: Week 27

Written By
thumbnail
Joe Burns
Joe Burns
Jan 4, 2005


Another Tip of the Hat…

Does anybody really know what time it is?

Well, you do if you click the button below to see the clock run. This week we look at grabbing and altering the minute and second sections of the clock.

But first let’s look at the script and its result one more time.



Here’s the Code for the Clock

Man, that’s a neat effect. Here’s the code that grabs the minutes and seconds:


var min = RightNow.getMinutes()
if (min < 10)
{nmin = “0” +min}
else
{nmin = min}

var sec = RightNow.getSeconds()

if (sec < 10)
{nsec = “0” +sec}
else
{nsec = sec}

if (nsec >= 60)
{nnsec = “00”}
else
{nnsec = nsec}


We get the minute and second by calling for them through the use of RightNow.getMinutes() and RightNow.getSeconds(). Remember from Script Tip #26, we set up RightNow as the new Date(). We’re using the two methods to extract the minutes and seconds.

The Minutes

Let’s start with the minutes. The return is correct in terms of numbers, there’s no need to add one to the return, but we do have to make one small change. And we, again, do it with an If statement.

Here’s the deal. When the minute is less than 10, the return is a single digit. That doesn’t look good. The user expects to see double digits no matter what the number. So, we set up the statement, that if the minute’s return is less than 10, then add a zero in front and display. If the return is more than 10, do nothing. It looks like this:

if (min < 10)
{nmin = “0” +min}
else
{nmin = min}

Boom. We’re done with the minutes. Let’s do the seconds.

The Seconds

We’ll do two things to the seconds. The first is exactly what we did with the minutes. If the second return is less than 10, add a zero in front and display.

The second alteration to the seconds return follows the same If statement format. However, this If statement will only come into play one time a minute.

One of the strangest things about the seconds return is that when the second “turns over” to count again, it always starts at 1. That means “60” is the highest number. Well, thatdoesn’t look right. It should read “00”, right? Yes! So, let’s set up an If statement that checks the seconds for the number “60”. If it finds “60” it will change it into “00” and display the double zero. The code looks like this:

if (nsec >= 60)
{nnsec = “00”}
else
{nnsec = nsec}

Hey! There ya go! Minutes and seconds! Now they look great and display well. It’s starting to look like a clock.

Next Week: Setting A Time Out!


Learn to write your own JavaScripts with the
JavaScript Goodies!

You can find many other uses for JavaScript
on your Web pages here!

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.