HTML and CSS Reference
In-Depth Information
Introducing Object Constructors
The command you employed to create and store a date uses a JavaScript statement
known as an object constructor and has the general syntax
new Object ( parameters )
where Object is the name of the class of object and parameters is a list of parameter
values associated with that object class. The variable that is created from the object
constructor is an object instance . Thus, the statement
var today = new Date();
creates an object instance in the today variable based on the Date class of objects.
You also can create a date using the command
variable = new Date( year , month , day , hours , minutes , seconds );
where year , month , day , hours , minutes , and seconds are the values of the date and
time, and the month value is an integer from 0 to 11, where 0 = January, 1 = February,
and so forth. Time values again are expressed in 24-hour time. Thus, the following com-
mand creates a variable storing the date and time February 24, 2015, at 2:35:05 p.m.:
thisDate = new Date(2015, 1, 24, 14, 35, 5);
Creating and Storing a Date
• To create a Date object, use the object constructor
new Date(“ month day , year hours : minutes : seconds ”)
where month , day , year , hours , minutes , and seconds indicate the date and time
to be stored in the Date object. Time values are entered in 24-hour time.
• To create a Date object with numeric values, use the JavaScript command
new Date(year, month, day, hours, minutes, seconds)
where year , month , day , hours , minutes , and seconds are the values of the date
and time, and the month value is an integer from 0 to 11, where 0 = January,
1 = February, and so forth. Time values are entered in 24-hour time.
• To create a Date object containing the current date and time, use the following
object constructor:
new Date()
Now that you've seen how to store date and time information in a variable, you'll cre-
ate a variable named today that stores a Date object. You'll use February 24, 2015 as the
initial date and 2:35:05 p.m. as the initial time. Later in this tutorial, you'll set the value
of the today variable to the current date and time. For now, using a preset date and time
lets you check that any calculations based on this date are correct. You'll add the today
variable to the NYClock() function you just created.
Search WWH ::




Custom Search