Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Object-Oriented JavaScript - Second Edition

You're reading from   Object-Oriented JavaScript - Second Edition If you've limited or no experience with JavaScript, this book will put you on the road to being an expert. A wonderfully compiled introduction to objects in JavaScript, it teaches through examples and practical play.

Arrow left icon
Product type Paperback
Published in Jul 2013
Publisher Packt
ISBN-13 9781849693127
Length 382 pages
Edition 2nd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Stoyan STEFANOV Stoyan STEFANOV
Author Profile Icon Stoyan STEFANOV
Stoyan STEFANOV
Arrow right icon
View More author details
Toc

Table of Contents (19) Chapters Close

Object-Oriented JavaScript Second Edition
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
1. Object-oriented JavaScript FREE CHAPTER 2. Primitive Data Types, Arrays, Loops, and Conditions 3. Functions 4. Objects 5. Prototype 6. Inheritance 7. The Browser Environment 8. Coding and Design Patterns Reserved Words Built-in Functions
Built-in Objects 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(2015, 0, 1, 13, 30, 35, 505);
Thu Jan 01 2015 13:30:35 GMT-0800 (PST)
  • 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(1420147835505);
    Thu Jan 01 2015 13:30:35 GMT-0800 (PST)
    
  • If you don't pass anything, the current date/time is assumed:

    > new Date();
    Fri Jan 11 2013 12:20:45 GMT-0800 (PST)
    
  • If you pass a string, it's parsed in an attempt to extract a possible date value:

    > new Date('May 4, 2015');
    Mon May 04 2015 00:00:00 GMT-0700 (PDT)
    

Omitting new gives you a string version of the current date:

> Date() === new Date().toString();
true

Members of the Date constructor

Property/method

Description

Date.parse(string)

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

> Date.parse('May 5, 2015');
1430809200000
> 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(2015, 0, 1, 13, 30, 35, 505);
1420119035505

The Date.prototype members

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(2015, 0, 1);
> d.toString();
"Thu Jan 01 2015 00:00:00 GMT-0800 (PST)"
> d.toUTCString();
"Thu, 01 Jan 2015 08:00:00 GMT"

toDateString()

Returns only the date portion of toString():

> new Date(2015, 0, 1).toDateString();
"Thu Jan 01 2010"

toTimeString()

Returns only the time portion of toString():

> new Date(2015, 0, 1).toTimeString();
"00:00:00 GMT-0800 (PST)"

toLocaleString()

toLocaleDateString()

toLocaleTimeString()

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

> new Date(2015, 0, 1).toString();
"Thu Jan 01 2015 00:00:00 GMT-0800 (PST)"
> new Date(2015, 0, 1).toLocaleString();
"1/1/2015 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(2015, 0, 1);
> d.getTime();
1420099200000
> d.setTime(d.getTime()+ 1000 * 60 * 60 * 24);
1420185600000
> d.toLocaleString();
"Fri Jan 02 2015 00:00:00 GMT-0800 (PST)"

getFullYear()

getUTCFullYear()

setFullYear(year, month, date)

setUTCFullYear(year, month, da te)

Get or set a full year using local or UTC time. There is also getYear() but it is not Y2K compliant, so use getFullYear() instead.

> var d = new Date(2015, 0, 1);
> d.getYear();
115
> d.getFullYear();
2015
> d.setFullYear(2020);
1577865600000
> d;
Wed Jan 01 2020 00:00:00 GMT-0800 (PST)

getMonth()

getUTCMonth()

setMonth(month, date)

setUTCMonth(month, date)

Get or set the month, starting from 0 (January):

> var d = new Date(2015, 0, 1);
> d.getMonth();
0
> d.setMonth(11);
1448956800000
> d.toLocaleDateString();
"12/1/2015"

getDate()

getUTCDate()

setDate(date)

setUTCDate(date)

Get or set the date of the month.

> var d = new Date(2015, 0, 1);
> d.toLocaleDateString();
"1/1/2015"
> d.getDate();
1
> d.setDate(31);
1422691200000
> d.toLocaleDateString();
"1/31/2015"

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 or set the hour, minutes, seconds, milliseconds, all starting from 0.

> var d = new Date(2015, 0, 1);
> d.getHours() + ':' + d.getMinutes();
"0:0"
> d.setMinutes(59);
1420102740000
> 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();
480
> 420 / 60; // hours
8

getDay()

getUTCDay( )

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

> var d = new Date(2015, 0, 1); 
> d.toDateString();
"Thu Jan 01 2015"
> d.getDay();
4
> var d = new Date(2015, 0, 4); 
> d.toDateString();
"Sat Jan 04 2015"
> d.getDay();
0

ECMAScript 5 additions to Date

Property/method

Description

Date.now()

A convenient way to get the current timestamp:

> Date.now() === new Date().getTime();
true

Date.prototype.toISOString()

Yet another toString().

> var d = new Date(2015, 0, 1);
> d.toString();
"Thu Jan 01 2015 00:00:00 GMT-0800 (PST)"
> d.toUTCString();
"Thu, 01 Jan 2015 08:00:00 GMT"
> d.toISOString();
"2015-01-01T00:00:00.000Z"

Date.prototype.toJSON()

Used by JSON.stringify() (refer to the end of this appendix) and returns the same as toISOString().

> var d = new Date();
> d.toJSON() === d.toISOString();
true
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime
Visually different images