04.11.2015 Views

javascript

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

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

Chapter 5: Reference Types<br />

To create a date object, use the new operator along with the Date constructor, like this:<br />

var now = new Date();<br />

When the Date constructor is used without any arguments, the created object is assigned the current<br />

date and time. To create a date based on another date or time, you must pass in the millisecond<br />

representation of the date (the number of milliseconds after midnight, January 1, 1970 UTC). To aid in<br />

this process, ECMAScript provides two methods: Date.parse() and Date.UTC() .<br />

The Date.parse() method accepts a string argument representing a date. It attempts to convert the<br />

string into a millisecond representation of a date. ECMA - 262 doesn ’ t define which date formats Date<br />

.parse() should support, so its behavior is implementation - specific and often locale - specific. Browsers<br />

in the United States typically accept the following date formats:<br />

❑<br />

❑<br />

❑<br />

month/date/year (such as 6/13/2004)<br />

month_name date, year (such as January 12, 2004)<br />

day_of_week month_name date year hours:minutes:seconds time_zone (such as Tue May 25<br />

2004 00:00:00 GMT - 0700)<br />

For instance, to create a date object for May 25, 2004, the following code can be used:<br />

var someDate = new Date(Date.parse(“May 25, 2004”));<br />

If the string passed into Date.parse() doesn ’ t represent a date, then it returns NaN . The Date<br />

constructor will call Date.parse() behind the scenes if a string is passed in directly, meaning that the<br />

following code is identical to the previous example:<br />

var someDate = new Date(“May 25, 2004”);<br />

This code produces the same result as the previous example.<br />

There are a lot of quirks surrounding the Date type and its implementation in<br />

various browsers. There is a tendency to replace out - of - range values with the<br />

current value to produce an output, so when trying to parse “ January 32,<br />

2007 ” , some browsers will interpret it as “ February 1, 2007 ” , whereas Opera<br />

tends to insert the current day of the current month, returning “ January<br />

current_day, 2007 ” . This means running the code on September 21 returns<br />

“ January 21, 2007 ” .<br />

The Date.UTC() method also returns the millisecond representation of a date, but constructs that value<br />

using different information than Date.parse() . The arguments for Date.UTC() are the year, the zero -<br />

based month (January is 0, February is 1, and so on), the day of the month (1 through 31), and the hours<br />

(0 through 23), minutes, seconds, and milliseconds of the time. Of these arguments, only the first two<br />

(year and month) are required. If the day of the month isn ’ t supplied, it ’ s assumed to be 1, while all other<br />

omitted arguments are assumed to be 0. Here are two examples of Date.UTC() in action:<br />

110

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

Saved successfully!

Ooh no, something went wrong!