Dave's Brain

Browse - Programming Tips - How do I display the current local time?

Date: 2010may4
Language: javaScript
Platform: web
Level: beginner

Q.  How do I display the current local time?

A.  Here are two ways:

function showTime() { var 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

Add a comment

Sign in to add a comment
Copyright © 2008-2017, 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.