Book Image

Object-Oriented JavaScript

By : Stoyan Stefanov, Stoyan STEFANOV
Book Image

Object-Oriented JavaScript

By: Stoyan Stefanov, Stoyan STEFANOV

Overview of this book

Table of Contents (18 chapters)
Object-Oriented JavaScript
Credits
About the Author
About the Reviewers
Preface
Built-in Functions
Regular Expressions
Index

Date


The Date constructor can be used with several types of input:

  • You can pass values for year, month, date of the month, hour, minute, second and millisecond, like so:

    >>> new Date(2011, 0, 1, 13, 30, 35, 500)

    Sat Jan 01 2011 13:30:35 GMT-0800 (Pacific Standard Time)

  • You can skip any of the input parameters, in which case they are assumed to be 0. Note that month values are from 0 (January) to 11 (December), hours are from 0 to 23, minutes and seconds 0 to 59, and milliseconds 0 to 999.

  • You can pass a timestamp:

    >>> new Date(1293917435500)

    Sat Jan 01 2011 13:30:35 GMT-0800 (Pacific Standard Time)

  • If you don't pass anything, the current date/time is assumed:

    >>> new Date()

    Fri Apr 18 2008 01:13:00 GMT-0700 (Pacific Daylight Time)

  • If you pass a string, it's parsed in attempt to extract a possible date value:

    >>> new Date('May 4, 2008')

    Sun May 04 2008 00:00:00 GMT-0700 (Pacific Daylight Time)

Members of the Date Constructor

Property/Method

Description

Date.parse(string)

Similar to passing a string to new Date(), this method parses the input string in attempt to extract a valid date value. Returns a timestamp on success, NaN on failure:

>>> Date.parse('May 4, 2008')

1209884400000

>>> Date.parse('4th')

NaN

Date.UTC(year, month, date, hours, minutes, seconds, ms)

Returns a timestamp but in UTC (Coordinated Universal Time), not in local time.

>>> Date.UTC(2011, 0, 1, 13, 30, 35, 500)

1293888635500

Members of the Date Objects

Property/Method

Description/Example

toUTCString()

Same as toString() but in universal time. Here's how Pacific Standard (PST) local time differs from UTC:

>>> var d = new Date(2010, 0, 1);
>>> d.toString()

"Fri Jan 01 2010 00:00:00 GMT-0800 (Pacific Standard Time)"

>>> d.toUTCString()

"Fri, 01 Jan 2010 08:00:00 GMT"

toDateString()

Returns only the date portion of toString():

>>> new Date(2010, 0, 1).toDateString();

"Fri Jan 01 2010"

toTimeString()

Returns only the time portion of toString():

>>> new Date(2010, 0, 1).toTimeString();

"00:00:00 GMT-0800 (Pacific Standard Time)"

toLocaleString()

toLocaleDateString()

toLocaleTimeString()

Equivalent to toString(), toDateString(), and toTimeString() respectively, but in a friendlier format, according to the current user's locale.

>>> new Date(2010, 0, 1).toString();

"Fri Jan 01 2010 00:00:00 GMT-0800 (Pacific Standard Time)"

>>> new Date(2010, 0, 1).toLocaleString();

"Friday, January 01, 2010 12:00:00 AM"

getTime()setTime(time)

Get or set the time (using a timestamp) of a date object. The following example creates a date and moves it one day forward:

>>> var d = new Date(2010, 0, 1);
>>> d.getTime();

1262332800000

>>> d.setTime(d.getTime()+ 1000 * 60 * 60 * 24);

1262419200000

>>> d.toLocaleString()

"Saturday, January 02, 2010 12:00:00 AM"

getFullYear()

getUTCFullYear()

setFullYear(year, month, date)

setUTCFullYear(year, month, date)

Get/Set a full year using local or UTC time. There is also getYear() but it is not Y2K compliant, so getFullYear() should be used.

>>> var d = new Date(2010, 0, 1);
>>> d.getYear()

110

>>> d.getFullYear()

2010

>>> d.setFullYear(2011)

1293868800000

>>> d

Sat Jan 01 2011 00:00:00 GMT-0800 (Pacific Standard Time)

getMonth()

getUTCMonth()

setMonth(month, date)

setUTCMonth(month, date)

Get/Set month, starting from 0 (January):

>>> var d = new Date(2010, 0, 1);
>>> d.getMonth()

0

>>> d.setMonth(11)

1291190400000

>>> d.toLocaleDateString()

"Wednesday, December 01, 2010"

getDate()

getUTCDate()

setDate(date)

setUTCDate(date)

Get/Set date of the month.

>>> var d = new Date(2010, 0, 1);
>>> d.toLocaleDateString() 

"Friday, January 01, 2010"

>>> d.getDate();

1

>>> d.setDate(31);

1264924800000

>>> d.toLocaleDateString()

"Sunday, January 31, 2010"

getHours()

getUTCHours()

setHours(hour, min, sec, ms)

setUTCHours(hour, min, sec, ms)

getMinutes()

getUTCMinutes()

setMinutes(min, sec, ms)

setUTCMinutes(min, sec, ms)

getSeconds()

getUTCSeconds()

setSeconds(sec, ms)

setUTCSeconds(sec, ms)

getMilliseconds()

getUTCMilliseconds()

setMilliseconds(ms)

setUTCMilliseconds(ms)

Get/Set hour, minutes, seconds, milliseconds, all starting from 0.

>>> var d = new Date(2010, 0, 1);
>>> d.getHours() + ':' + d.getMinutes()

"0:0"

>>> d.setMinutes(59)

1262336399000

>>> d.getHours() + ':' + d.getMinutes()

"0:59"

getTimezoneOffset()

Returns the difference between local and universal (UTC) time, measured in minutes. For example the difference between PST (Pacific Standard Time) and UTC:

>>> new Date().getTimezoneOffset()

420

>>> 420/60

7

getDay()

getUTCDay()

Returns the day of the week, starting from 0 (Sunday):

>>> var d = new Date(2010, 0, 1); 
>>> d.toLocaleDateString() 

"Friday, January 01, 2010"

>>> d.getDay() 

5

>>> var d = new Date(2010, 0, 3); 
>>> d.toLocaleDateString() 

"Sunday, January 03, 2010"

>>> d.getDay() 

0