The ES6 Date is defined as the number of milliseconds that have been passed since midnight on January 1, 1970, UTC. Date objects can be created by the new Date() constructor. In JavaScript, both date and time are represented by the Date object.
Javascript Date Constructors: In JavaScript, a constructor gets called when an object is created using the new keyword.
Constructors | Description |
---|---|
Date() |
Creates a date object for the current time and date var date = new Date(); |
Date(milliseconds) |
Creates a date object with a time equal to the number of milliseconds passed from January 1, 1970, UTC. var date = new Date(1570991017113); |
Date(datestring) |
Creates a date object with the specified date string, then it is parsed automatically the same as Date.parse() does var date = new Date("2019-10-11") |
Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]) |
Creates a date object with the specified date in the local time zone. The first two arguments are necessary and the rest are optional. var date = new Date(2019, 0, 11, 15, 45, 55, 55) |
Instance Properties: An instance property is a property that has a new copy for every new instance of the class
Properties | Description |
---|---|
Date.prototype | It represents the prototype for the Date constructor. Date.prototype is itself an ordinary object rather than the Date instance. It allows adding properties to the Date object |
Date.constructor | It returns the reference of the array function that created the instance of the Date object |
Static Methods: If the method is called using the date class itself then it is called a static method
Method | Description |
---|---|
Date.now() |
It returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC. console.log(Date.now()); // 1571060400486 // i.e. number of milliseconds elapsed since January 1, 1970 00:00:00 UTC |
Date.parse() |
It returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC for provided string representation console.log(Date.parse("2019-10-11")); // 1570800633000 |
Date.UTC() |
It returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC for provided date separated by a comma. console.log(Date.parse("2019, 9, 11")); // 1570800633000 |
Date.prototype Methods: If the method is called on an instance of a date then it is called an instance method
Methods | Description |
---|---|
getDate() |
It returns the date of the month from the given date object. var d = new Date("2019-10-11"); |
getDay() |
It returns the day of the week from 0 to 6 according to the local time of the given date object. the var d = new Date("2019-10-11"); |
getFullYear() |
It returns the full year of the given date object. var d = new Date("2019-10-11"); |
getHours() |
It returns the hour (0-23) of the given date object. var d = new Date("2019-10-11 16:30:33") |
getMilliseconds() |
It returns the milliseconds (0-999) of the given date object according to the local time. var d = new Date("October 11, 2019 16:30:33"); |
getMinutes() |
It returns the minute (0-59) of the given date object according to the local time. var d = new Date("October 11, 2019 16:30:33") |
getMonth() |
It returns the month (0-11) of the given date object according to the local time. var d = new Date("October 11, 2019 16:30:33") |
getSeconds() |
It returns the seconds (0-59) of the given date object according to the local time. var d = new Date("October 11, 2019 16:30:33") |
getTime() |
It returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC., var d = new Date("October 11, 2019 16:30:33") |
getTimezoneOffset() |
It returns the difference between UTC and local timezone in minutes. var d = new Date("October 11, 2019 16:30:33"); |
getUTCDate() |
It returns the day of the month (1-31) according to the universal time of the given date object. var d = new Date("October 11, 2019 16:30:33"); |
getUTCDay() |
It returns the day of the week (0-6) according to the universal time of the given date object. var d = new Date("October 11, 2019 16:30:33"); |
getUTCFullYear() |
It returns the year according to the universal time of the given date object. var d = new Date("October 11, 2019 16:30:33"); |
getUTCHours() |
It returns the hour (0-23) according to the universal time of the given date object. var d = new Date("October 11, 2019 16:30:33"); |
getUTCMilliseconds() |
It returns the milliseconds (0-999) according to the universal time of the given date object. var d = new Date("October 11, 2019 16:30:33"); |
getUTCMinutes() |
It returns the minutes according to the universal time of the given date object. var d = new Date("October 11, 2019 16:30:33"); |
getUTCMonth() |
It returns the month (0-11) according to the universal time of the given date object. var d = new Date("October 11, 2019 16:30:33"); |
getUTCSeconds() |
It returns seconds (0-59) according to the universal time of the given date object. var d = new Date("October 11, 2019 16:30:33") |
setDate() |
It is used to set the date of a month to the date object in local time. var d = new Date("October 11, 2019 16:30:33"); |
setFullYear() |
It is used to set the year to the date object in local time. var d = new Date("October 11, 2019 16:30:33"); |
setHours() |
It is used to set hour (0-23) to the date object in local time. var d = new Date("October 11, 2019 16:30:33"); d.setHours(19); console.log(d.getHours()); // 19 |
setMilliseconds() |
It is used to set milliseconds to the date object in local time. var d = new Date("October 11, 2019 16:30:33"); d.setMilliseconds(200); console.log(d.getMilliseconds()); // 200 |
setMinutes() |
It is used to set minutes (0-59) to the date object in local time. var d = new Date("October 11, 2019 16:30:33"); d.setMinutes(20); console.log(d.getMinutes()); // 20 |
setMonth() |
It is used to set month (0-11) to the date object in local time. var d = new Date("October 11, 2019 16:30:33"); d.setMonth(8); console.log(d.getMonth()); // 8 |
setSeconds() |
It is used to set seconds (0-59) to the date object in local time. var d = new Date("October 11, 2019 16:30:33"); d.setSeconds(34); console.log(d.getSeconds()); // 34 |
setTime() |
It is used to set milliseconds after January 1, 1970, 00:00:00 UTC to the date object in local time., var d = new Date("October 11, 2019 16:30:33"); d.setTime(1570815753000); console.log(d.toDateString()); // "Fri Oct 11 2019" |
setUTCDate() |
It is used to set the date of the month (0-11) to the date object according to universal time. var d = new Date("October 11, 2019 16:30:33"); d.setUTCDate(12); console.log(d.getDate()); // 12 |
setUTCFullYear() |
It is used to set the year to the date object according to universal time. var d = new Date("October 11, 2019 16:30:33"); d.setUTCFullYear(2018); console.log(d.getFullYear()); // 2018 |
setUTCHours() |
It is used to set hours (0-23) to the date object according to universal time. var d = new Date("October 11, 2019 16:30:33"); d.setUTCHours(16); console.log(d.getHours()); // 21 |
setUTCMilliseconds() |
It is used to set milliseconds to the date object according to universal time. var d = new Date("October 11, 2019 16:30:33"); d.setUTCMilliseconds(166); console.log(d.getMilliseconds()); // 166 |
setUTCMinutes() |
It is used to set minutes (0-59) to the date object according to universal time. var d = new Date("October 11, 2019 16:30:33"); d.setUTCMinutes(22); console.log(d.getMinutes()); // 52 |
setUTCMonth() |
It is used to set month (0-11) to the date object according to universal time. var d = new Date("October 11, 2019 16:30:33"); d.setUTCMonth(8); console.log(d.getMonth()); // 8 |
setUTCSeconds() |
It is used to set seconds (0-59) to the date object according to universal time. var d = new Date("October 11, 2019 16:30:33"); d.setUTCSeconds(10); console.log(d.getSeconds()); // 10 |
Date Conversion Methods: The methods that are used to convert dates from one format to others.
Methods | Description |
---|---|
toDateString() |
It returns the string representation for the date portion of the date object. var d = new Date(2019, 9, 11, 19, 30, 33); |
toISOString() |
It returns the string representation for the date portion of the date object using ISO 8601 extended format. var d = new Date(2019, 9, 11, 19, 30, 33); |
toJSON() |
It returns the string representation for the date portion of the date object. var d = new Date(2019, 9, 11, 19, 30, 33); |
toLocaleDateString() |
It takes two parameters (optional) – locale and options and returns the string representation for the date portion according to the specified locale. var d = new Date(2019, 9, 11, 19, 30, 33); |
toLocaleString() |
It returns the string of the date object of the date portion in locale format. var d = new Date(2019, 9, 11, 19, 30, 33); |
toLocaleTimeString() |
It returns the string representation for the time portion of the date object. var d = new Date(2019, 9, 11, 19, 30, 33); |
toString() |
It returns the string representation for the date of the date object. var d = new Date(2019, 9, 11, 19, 30, 33); |
toTimeString() |
It returns the string representation for the time portion of the date object. var d = new Date(2019, 9, 11, 19, 30, 33); |
toUTCString() |
It returns the string representation for the date object in the format of a universal timezone. var d = new Date(2019, 9, 11, 19, 30, 33); |
valueOf() |
It returns the number of milliseconds elapsed from January 1, 1970, 00:00:00 UTC to the date provided., var d = new Date(2019, 9, 11, 19, 30, 33); |