Basic JavaScript Date and Time Functions
By Will Bontrager2004-08-12
The Date
To get the date, the complete working example further below has a function called getCalendarDate()
The first 13 lines of the function creates an array containing the names of the calendar month. The value obtained from the now.getMonth() function is used to determine which month name to use. The line that does that is
var monthname = months[monthnumber];
When now.getMonth() extracts a month number, it assumes January is number 0 and December is number 11. If you prefer to print the number of the month instead of the name of the month, replace the above line with
monthnumber = monthnumber + 1;
to conform the month number with the way most humans count them (January = 1, etc.).
The getCalendarDate() function also has a line to adjust the year if the visitor is using an older browser that still assumes we're living in the 1900's. You'll recognize the line when you read the code.
getCalendarDate() constructs a string of characters representing the calendar date. It then returns that construction to whatever JavaScript code calls the function.
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
