Create Your Own jQuery Digital Clock

Sam Deering
Sam Deering
Share

This is a pretty cool way to display the time on your web page using jQuery to get the current time and then refresh the clock every 1 second.

function updateClock ( )
 	{
 	var currentTime = new Date ( );
  	var currentHours = currentTime.getHours ( );
  	var currentMinutes = currentTime.getMinutes ( );
  	var currentSeconds = currentTime.getSeconds ( );

  	// Pad the minutes and seconds with leading zeros, if required
  	currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  	currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

  	// Choose either "AM" or "PM" as appropriate
  	var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

  	// Convert the hours component to 12-hour format if needed
  	currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

  	// Convert an hours component of "0" to "12"
  	currentHours = ( currentHours == 0 ) ? 12 : currentHours;

  	// Compose the string for display
  	var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
  	
  	
   	$("#clock").html(currentTimeString);
   	  	
 }

$(document).ready(function()
{
   setInterval('updateClock()', 1000);
});

Frequently Asked Questions about Creating a jQuery Digital Clock

How can I customize the appearance of my jQuery digital clock?

Customizing the appearance of your jQuery digital clock can be done using CSS. You can change the font, color, size, and more. For example, if you want to change the color and size of the clock, you can use the following code:

#clock {
color: red;
font-size: 30px;
}
This will make the clock red and increase the font size to 30px. You can replace ‘red’ with any color you prefer and adjust the ’30px’ to the size you want.

Can I add a date to my jQuery digital clock?

Yes, you can add a date to your jQuery digital clock. You can use the JavaScript Date object to get the current date and then use jQuery to display it. Here’s an example of how you can do it:

var date = new Date();
$('#date').text(date.toDateString());
This will display the current date in a div with the id ‘date’. You can format the date in any way you prefer using the methods of the Date object.

How can I make my jQuery digital clock show the time in a 12-hour format?

By default, the JavaScript Date object returns the time in a 24-hour format. However, you can convert it to a 12-hour format using some simple calculations. Here’s how you can do it:

var hours = date.getHours();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
This will convert the hours to a 12-hour format and determine whether it’s am or pm.

Can I add a ticking sound to my jQuery digital clock?

Yes, you can add a ticking sound to your jQuery digital clock. You can use the HTML5 audio element and jQuery to play a sound every second. Here’s an example of how you can do it:

var tick = new Audio('tick.mp3');
setInterval(function() {
tick.play();
}, 1000);
This will play the ‘tick.mp3’ sound every second. Make sure to replace ‘tick.mp3’ with the path to your sound file.

How can I add a timezone to my jQuery digital clock?

Adding a timezone to your jQuery digital clock can be a bit tricky because JavaScript gets the time from the user’s system. However, you can use a library like Moment.js to easily handle timezones. Here’s an example of how you can do it:

var time = moment().tz("America/New_York").format('HH:mm:ss');
$('#clock').text(time);
This will display the time in New York. You can replace ‘America/New_York’ with any timezone you want.

How can I make my jQuery digital clock more accurate?

To make your jQuery digital clock more accurate, you can synchronize it with a server time. This can be done using AJAX to get the server time and then use it to set the clock. However, this requires a server-side script to return the time.

Can I use jQuery to create an analog clock?

Yes, you can use jQuery to create an analog clock. However, it’s a bit more complex than creating a digital clock because you need to calculate the positions of the hands based on the time. You can use the trigonometric functions in JavaScript to do this.

How can I add a countdown timer to my jQuery digital clock?

You can add a countdown timer to your jQuery digital clock by subtracting the current time from the target time. You can use the JavaScript Date object to get the current time and then calculate the difference.

Can I add a stopwatch to my jQuery digital clock?

Yes, you can add a stopwatch to your jQuery digital clock. You can use the JavaScript setInterval function to increase a counter every second and then display it using jQuery.

How can I make my jQuery digital clock responsive?

Making your jQuery digital clock responsive can be done using CSS. You can use media queries to adjust the size and layout of the clock based on the screen size.