Dates

JavaScript Date constructor helps in managing, displaying and parsing the data. The date API uses the term UTC ( Coordinated Universal Time) and it is the synonym of GMT ( Greenwich Mean Time) i.e, nearly to the time zone of London, UK.

The Date Constructor :

Date Constructor is can be invoked as follows :

new Date(year, month, date, hours, minutes, seconds, milliseconds);

This constructs a new Date from the given date. The parameters should have the ranges up to :

  • year : 0 <= year <= 99 (1900 is also added).
  • month : 0 to 11 (0 - January)
  • date  : 1-31
  • hours : 0-23
  • minutes : 0-59
  • seconds : 0-59
  • milliseconds : 0-999
Example :

new Date(2017,4,3,12,59,59,59);
Wed May 03 2017 12:59:59 GMT+0530

JavaScript has inherited the convention of interpreting 0-January,1-february and so on from Java.

new Date(dateStr) :

It parses a string with the date time string and then returns a time value.

For Example :
new Date('2017-04-03');
Mon Apr 03 2017 05:30:00 GMT+0530

If the date sent is illegal then NaN will be passed to the new Date(number).

new Date(timeValue)

This creates a date by passing the number of milliseconds as parameter and it is been identified from the year 1970 January 1st.

new Date(0)
Thu Jan 01 1970 05:30:00 GMT+0530

We can even get the inverse() of this constructor using the getTime() method, which will return back in milliseconds.

new Date(12345).getTime();
12345

NaN can also be used as an argument and it returns a special instance of Date i.e, an "invalid date".
var dat = new Date();
dat.getTime();
NaN
dat.getYear();
NaN
dat.toString()
Invalid Date
dat.toJSON()
null

new Date()

It creates a new date and time, it works same as new Date(Date.now()).

Constructor Methods For Date :
  • Date.now() : This returns the current date and time in milliseconds from 1st January 1970 00:00:00 UTC and it returns same result as new Date().getTime().
  • Date.parse(dateString) : This converts the dateString into milliseconds from 1st January 1970 00:00:00 UTC. 
       For Example :

        Date.parse('2017-04-03');
        1491177600000
        Date.parse('1970-01-01');
        0
If it can't able to parse the string, then it returns NaN.

Date.parse('abcd');
NaN

Date.UTC(year,month,date,hours,minutes,seconds,milliseconds) :
This doesn't return the date format, it converts the given date into the milliseconds from 1st January 1970 00:00:00 UTC.

For Example :

Date.UTC(2017,4,3,12,59,59,59);
1493816399059

Date Prototype Methods :

Getters and Setters Method for time units :

  • Local time :
            Date.prototype.get<>()returns Unit, according to the local time.
            Date.prototype.set<>(number) : This will set the Unit, according to the local time.
  • Universal time :
            Date.prototype.getUTC<>()returns Unit, according to the universal time.
            Date.prototype.setUTC<>() : This will set the Unit, according to the universal time.

Unit can be :
  • Full Year : four digit
  • Month : 0-11
  • Date : 0-31
  • Day (only getter) : 0-6 (0 is Sunday)
  • Hours : 0-23
  • Minutes : 0-59
  • Seconds : 0-59
  • Milliseconds : 0-999
For Example :

var dat = new Date('2017-04-15');
dat
Sat Apr 15 2017 05:30:00 GMT+0530
dat.getDate()
15

dat.getDay()
6

Getter and Setter Methods :

The following methods will get and set the time in milliseconds from 1st January 1970.

  • Date.prototye.getTime() : It returns the milliseconds from 1st January 1970 00:00:00 UTC.
  • Date.prototye.setTime(ms) : It sets the date as specified in milliseconds from 1st January 1970 00:00:00 UTC.
  • Date.prototye.valueOf() : This is same as the getTime() and this is called when getTime() is converted to number.
  • Date.prototye.getTimezoneOffset() : This returns the difference between local time and UTC time in minutes.
  • Date.prototye.getYear() :  This  method is been deprecated, instead of this we use getFullYear().
  • Date.prototye.setYear() : This method is also been deprecated, instead of this we use setFullYear().
Converting Date to a String :

Time in human-readable format :

  • toTimeString() :
             var a = new Date();
             a.toTimeString();
            "11:41:28 GMT+0530 (India Standard Time)"
  • toLocaleTimeString() : 
             a.toLocaleTimeString();
             "11:41:28"

Date in human-readable format :
  • toDateString() :
            a.toDateString();
            "Thu Dec 28 2017"
  • toLocaleDateString() :

            a.toLocaleDateString()
            "28/12/2017"

Date and Time in human readable format :
  • toString() :
            a.toString()
            "Thu Dec 28 2017 11:41:28 GMT+0530 (India Standard Time)"
  • toUTCString() :
           a.toUTCString()
           "Thu, 28 Dec 2017 06:11:28 GMT"

These all methods are been provided by the ECMAScript Internalisation API.

Date and Time in machine readable format :
  • toISOString() :
             a.toISOString()
             2017-12-28T06:11:28.552Z


Date Formats (no-time):

There date formats are:

YYYY-MM-DD
YYYY-MM
YYYY

YYYY-year, MM-month, DD-date

Time Formats (no-date) :

THH : mm:ss.sss
THH : mm:ss.sssZ

THH : mm:ss
THH : mm:ssZ

THH : mm
THH : mmZ


  • T is the prefix of the time part of a format.
  • HH is hours starts from 0-23 and 24 can also be given and it is hour 00 of the following day.
  • mm is minutes 0-59
  • ss is seconds 0-59
  • sss is milliseconds 0-999
  • z refers to the time zone i.e, Z for UTC and + or - will be followed by hh:mm
Arithmetic operations can also be done but leap seconds are ignored.

Example :


new Date('2017-04-13') - new Date('2017-04-03');
864000000





No comments:

Post a Comment

Overview of AngularJS

AngularJS is an open source web application framework. It is now maintained by Google . The most of the code can be eliminated by using th...