Dave's Brain

Browse - programming tips - javascript display the current time

Date: 2010may4
Language: javaScript
Level: beginner

Q.  How do I display the current local time time?

A.  Here are two ways:

	function showTime() {
		d, small_time, big_time;

		d = new Date;

		small_time = d.getHours() + ':' + zeroPad(d.getMinutes(), 2);
		alert(small_time);	// eg 14:04

		big_time = d.toLocaleTimeString();
		alert(big_time);	// eg 2:04:05 PM
	}

The helper function zeroPad() can be found here:
http://davekb.com/search.php?target=javascript+zeroPad
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.
Advertisements: