11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Table 7-1: Arguments to the Date() Constructor<br />

Argument Description Example<br />

"month dd, yyyy<br />

hh:mm:ss"<br />

Creates object with the date<br />

represented by the specified month,<br />

day (dd ),<br />

year ( yyyy ), hour (hh), minute<br />

(mm),<br />

and second (ss). Any omitted<br />

values<br />

are set to zero.<br />

Milliseconds Creates object with date<br />

represented as the integer number<br />

of milliseconds after the epoch.<br />

yyyy, mm, dd Creates object with the date<br />

specified by the integer values year<br />

(yyyy),<br />

month (mm), and day (dd).<br />

yyyy, mm, dd, hh, mm,<br />

ss<br />

yyyy, mm, dd, hh, mm,<br />

ss, ms<br />

Creates object with the date<br />

specified by the integer values for<br />

the year, month, day, hours,<br />

minutes, and seconds.<br />

Creates object with the date<br />

specified by the integer values for<br />

the year, month, day, hours,<br />

seconds, and milliseconds.<br />

var birthDay = new<br />

Date("March 24,<br />

1970");<br />

var someDate = new<br />

Date(795600003020);<br />

var birthDay = new<br />

Date(1970, 2, 24);<br />

var birthDay = new<br />

Date(1970, 2, 24,<br />

15, 0, 0);<br />

var birthDay = new<br />

Date(1970, 2, 24,<br />

15, 0, 250);<br />

Table 7-1 warrants some commentary. <strong>The</strong> string version of the constructor argument can be<br />

any date string that can be parsed by the Date.parse() method. In the syntax of the last two<br />

formats, the arguments beyond the year, month, and day are optional. If they are omitted, they<br />

are set to zero. <strong>The</strong> final syntax that includes milliseconds is available only in <strong>JavaScript</strong> 1.3+.<br />

Note Because of the ambiguity that arises from representing the year with two digits, you<br />

should always use four digits when specifying the year. This can be done using the<br />

getFullYear() method discussed later in this section.<br />

It is important to note that Date objects you create are static. <strong>The</strong>y do not contain a ticking<br />

clock. If you need to use a timer of some sort, the setInterval() and setTimeout() methods of<br />

the Window object are much more appropriate. <strong>The</strong>se other methods are discussed both in<br />

Appendix B and in later application-oriented chapters.<br />

Date objects are created to be picked apart and manipulated and to assist in formatting dates<br />

according to your specific application. You can even calculate the difference between two dates<br />

directly:<br />

var firstDate = new Date(1995, 0, 6);var secondDate = new Date(1999, 11, 2);var difference =<br />

secondDate - firstDate;alert(difference);<br />

<strong>The</strong> result indicates the approximate number of milliseconds elapsed between January 6, 1995,<br />

and December 2, 1999:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!