Skip to content
Related Articles

Related Articles

Improve Article

ES6 | Date

  • Last Updated : 23 Oct, 2019
Geek Week

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.

Constructors:

  1. Date() – Create a date object for the current time and date.
    var date = new Date();
    console.log(date);
    
  2. Date(milliseconds) – Create a date object with time equal to the number of milliseconds passed from January 1, 1970 UTC.
    var date = new Date(1570991017113);
    console.log(date);
    
  3. Date(datestring) – Create a date object with the specified date string, then it is parsed automatically same as Date.parse() does.



    var date = new Date("2019-10-11");
    console.log(date);
    
  4. Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]) – Create a date object with the specified date in 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);
    console.log(date);
    

Properties:

  1. Date.prototype: It represents the prototype for the Date constructor. Date.prototype is itself an ordinary object rather than the Date instance. It allows to add properties to the Date object.
  2. Date.constructor: It returns the reference of the array function that created the instance of the Date object.

Methods:

  1. 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
    
  2. Date.parse(): It returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC for provided string representation of date.
    console.log(Date.parse("2019-10-11")); // 1570800633000
    
  3. Date.UTC(): It returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC for provided date separated by comma.
    console.log(Date.parse("2019, 9, 11")); // 1570800633000
    

Date.prototype Methods:

  1. getDate(): It returns the date of the month from the given date object.
    var d = new Date("2019-10-11");
    console.log(d.getDate()); // 11
    
  2. getDay(): It returns the day of the week from 0 to 6 according to local time of the given date object.
    var d = new Date("2019-10-11");
    console.log(d.getDay()); // 5 i.e Friday
    
  3. getFullYear(): It returns the full year of the given date object.
    var d = new Date("2019-10-11");
    console.log(d.getFullYear()); // 2019
    
  4. getHours(): It returns the hour (0-23) of the given date object.
    var d = new Date("2019-10-11 16:30:33");
    console.log(d.getHours()); // 16
    
  5. 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");
    console.log(d.getMilliseconds()); // 0
    
  6. 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");
    console.log(d.getMinutes()); // 30
    
  7. 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");
    console.log(d.getMonth()); // 9
    
  8. 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");
    console.log(d.getSeconds()); // 33
    
  9. 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");
    console.log(d.getTime()); // 1570791633000
    
  10. getTimezoneOffset(): It returns the difference between UTC and local timezone in minutes.
    var d = new Date("October 11, 2019 16:30:33");
    console.log(d.getTimezoneOffset()); // -330
    
  11. getUTCDate(): It returns the day of the month (1-31) according to universal time of the given date object.
    var d = new Date("October 11, 2019 16:30:33");
    console.log(d.getUTCDate()); // 11
    
  12. getUTCDay(): It returns the day of the week (0-6) according to universal time of the given date object.
    var d = new Date("October 11, 2019 16:30:33");
    console.log(d.getUTCDay()); // 5
    
  13. getUTCFullYear(): It returns the year according to universal time of the given date object.
    var d = new Date("October 11, 2019 16:30:33");
    console.log(d.getUTCFullYear()); // 2019
    
  14. getUTCHours(): It returns the hour (0-23) according to universal time of the given date object.
    var d = new Date("October 11, 2019 16:30:33");
    console.log(d.getUTCHours()); // 11
    
  15. getUTCMilliseconds(): It returns the milliseconds (0-999) according to universal time of the given date object.
    var d = new Date("October 11, 2019 16:30:33");
    console.log(d.getUTCMilliseconds()); // 0
    
  16. getUTCMinutes(): It returns the minutes according to universal time of the given date object.
    var d = new Date("October 11, 2019 16:30:33");
    console.log(d.getUTCMinutes()); // 0
    
  17. getUTCMonth(): It returns the month (0-11) according to universal time of the given date object.
    var d = new Date("October 11, 2019 16:30:33");
    console.log(d.getUTCMonth()); // 9
    
  18. getUTCSeconds(): It returns seconds (0-59) according to universal time of the given date object.
    var d = new Date("October 11, 2019 16:30:33");
    console.log(d.getUTCSeconds()); // 33
    
  19. 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");
    d.setDate(13);
    console.log(d.getDate()); // 13
    
  20. setFullYear(): It is used to set year to the date object in local time.
    var d = new Date("October 11, 2019 16:30:33");
    d.setYear(2018);
    console.log(d.getFullYear()); // 2018
    
  21. 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
    
  22. 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
    
  23. 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
    
  24. 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
    
  25. 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
    
  26. 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"
    
  27. setUTCDate(): It is used to set 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
    
  28. setUTCFullYear(): It is used to set 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
    
  29. 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
    
  30. 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
    
  31. 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
    
  32. 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
    
  33. 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:

  1. toDateString(): It returns the string representation for the date portion of the date object.
    var d = new Date(2019, 9, 11, 19, 30, 33);
    console.log(d.toDateString()); // "Fri Oct 11 2019"
    
  2. 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);
    console.log(d.toISOString()); // "2019-10-11T14:00:33.000Z"
    
  3. toJSON(): It returns the string representation for the date portion of the date object.
    var d = new Date(2019, 9, 11, 19, 30, 33);
    console.log(d.toJSON()); // "2019-10-11T14:00:33.000Z"
    
  4. toLocaleDateString(): It takes two parameters (optional) – locale and options and returns the string representation for the date portion according to specified locale.
    var d = new Date(2019, 9, 11, 19, 30, 33);
    console.log(d.toLocaleDateString()); // "10/11/2019"
    
    // 10.2019
    console.log(d.toLocaleDateString("de-DE", {year:'numeric', month: 'numeric'}))
    
  5. toLocaleString(): It returns the string of date object of date portion in locale format.
    var d = new Date(2019, 9, 11, 19, 30, 33);
    console.log(d.toLocaleString()); // "10/11/2019, 7:30:33 PM"
    
  6. toLocaleTimeString(): It returns the string representation for the time portion of the date object.
    var d = new Date(2019, 9, 11, 19, 30, 33);
    console.log(d.toLocaleTimeString()); // "7:30:33 PM"
    
  7. toString(): It returns the string representation for date of the date object.
    var d = new Date(2019, 9, 11, 19, 30, 33);
    
    // "Fri Oct 11 2019 19:30:33 GMT+0530 (India Standard Time)"
    console.log(d.toString()); 
    
  8. toTimeString(): It returns the string representation for the time portion of the date object.
    var d = new Date(2019, 9, 11, 19, 30, 33);
    console.log(d.toTimeString()); // "19:30:33 GMT+0530 (India Standard Time)"
    
  9. toUTCString(): It returns the string representation for the date object in format of universal timezone.
    var d = new Date(2019, 9, 11, 19, 30, 33);
    console.log(d.toUTCString()); // "Fri, 11 Oct 2019 14:00:33 GMT"
    
  10. 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);
    console.log(d.valueOf()); // 1570802433000
    

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :