Book Image

Object-Oriented JavaScript - Third Edition

By : Ved Antani, Stoyan STEFANOV
5 (1)
Book Image

Object-Oriented JavaScript - Third Edition

5 (1)
By: Ved Antani, Stoyan STEFANOV

Overview of this book

JavaScript is an object-oriented programming language that is used for website development. Web pages developed today currently follow a paradigm that has three clearly distinguishable parts: content (HTML), presentation (CSS), and behavior (JavaScript). JavaScript is one important pillar in this paradigm, and is responsible for the running of the web pages. This book will take your JavaScript skills to a new level of sophistication and get you prepared for your journey through professional web development. Updated for ES6, this book covers everything you will need to unleash the power of object-oriented programming in JavaScript while building professional web applications. The book begins with the basics of object-oriented programming in JavaScript and then gradually progresses to cover functions, objects, and prototypes, and how these concepts can be used to make your programs cleaner, more maintainable, faster, and compatible with other programs/libraries. By the end of the book, you will have learned how to incorporate object-oriented programming in your web development workflow to build professional JavaScript applications.
Table of Contents (25 chapters)
Object-Oriented JavaScript - Third Edition
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Built-in Functions
Regular Expressions

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

Following are the 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 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

Following are the list of 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, date)

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 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 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/Set 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

Following are the additions to the Date constructor:

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