Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Object-Oriented JavaScript
  • Table Of Contents Toc
Object-Oriented JavaScript

Object-Oriented JavaScript

4.5 (48)
close
close
Object-Oriented JavaScript

Object-Oriented JavaScript

4.5 (48)

Overview of this book

Table of Contents (18 chapters)
close
close
Object-Oriented JavaScript
Credits
About the Author
About the Reviewers
Preface
2
Built-in Functions
4
Regular Expressions
5
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

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Object-Oriented JavaScript
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon