Java Reference
In-Depth Information
The Date Object
Date objects hold information about dates and times. Each object represents a single moment
in time.
Constructor Function
A constructor function is used to create a new date object using the
new
operator:
> today = new Date();
<< [object Date]{
}
This confirms that the variable
today
points to a
Date
object. To see what the date is, we
use the
toString()
method that all objects have:
> today.toString();
<< "Sun Jun 08 2014 15:43:03 GMT+0100 (BST)"
If an argument is not supplied, the date will default to the current date and time. It's possible
to create
Date
objects for any date by supplying it as an argument to the constructor func-
tion. This can be written as a string in a variety of forms:
> christmas = new Date('2014 12 25');
<< [object Date]{
}
> christmas.toString();
<< "Thu Dec 25 2014 00:00:00 GMT+0000 (GMT)"
> chanukah = new Date('16 December 2014');
<< [object Date]{
}
