Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Basic JavaScript Date and Time Functions

By Will Bontrager
2004-08-12


The Time

To get the time, the complete working example further below has a function called getClockTime()

getClockTime() includes code to format the clock time into an "AM" or "PM" representation. The following four lines accomplish that.

var ap = "AM";
if (hour > 11) { ap = "PM"; }
if (hour > 12) { hour = hour - 12; }
if (hour == 0) { hour = 12; }

If you want the clock time to represent a 24-hour clock instead of an "AM/PM" representation, simply remove those four lines from the function.

getClockTime() constructs a string of characters representing the clock time. It then returns that construction to whatever JavaScript code calls the function. If you don't want the construction to include the "AM" or "PM" part, remove the last two lines from the code that constructs the clock time.

Tutorial Pages:
» Basic JavaScript Date and Time Functions
» How To Extract Information From that Date Object
» The Date
» The Time
» The Printing
» The Complete Working Example


Copyright 2004 Bontrager Connection, LLC


 | Bookmark
Related Tutorials:
» JavaScript Debugging Techniques with Firebug
» Striped Tables Using JavaScript
» Opening PDFs in a New Window with JavaScript
» Essential Javascript -- A Javascript Tutorial
» Submit Forms Conditionally using JavaScript
» How to Setup a Randomising Function

Ask A Question
characters left.