Book Image

Object-Oriented JavaScript - Second Edition - Second Edition

Book Image

Object-Oriented JavaScript - Second Edition - Second Edition

Overview of this book

JavaScript is the behavior, the third pillar in today's paradigm that looks at web pages as something that consists of clearly distinguishable parts: content (HTML), presentation (CSS) and behavior (JavaScript). Using JavaScript, you can create not only web pages but also desktop widgets, browser and application extensions, and other pieces of software. It's a pretty good deal: you learn one language and then code all kinds of different applications. While there's one chapter specifically dedicated to the web browser environment including DOM, Events and AJAX tutorials, the rest is applicable to the other environments Many web developers have tried coding or adopting some bits of JavaScript, but it is time to "man up" and learn the language properly because it is the language of the browser and is, virtually, everywhere. This book starts from zero, not assuming any prior JavaScript programming knowledge and takes you through all the in-depth and exciting futures hidden behind the facade. Once listed in the "nice to have" sections of job postings, these days the knowledge of JavaScript is a deciding factor when it comes to hiring web developers. After reading this book you'll be prepared to ace your JavaScript job interview and even impress with some bits that the interviewer maybe didn't know. You should read this book if you want to be able to take your JavaScript skills to a new level of sophistication.
Table of Contents (19 chapters)
Object-Oriented JavaScript Second Edition
Credits
About the Authors
About the Reviewer
www.PacktPub.com
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(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