JS Startup

Challenges, Tips & Tricks & Tutorials

Javascript Date Method That You Must Know

Javascript Date Method That You Must Know

In this javascript tutorial, you’ll learn some important javascript date method that you mush know because playing with date manipulation required knowledge of the date method to fetch the required detail from javascript date object or set the detail in the date object.

JavaScript Date

JavaScript Date object represents a single moment in time. Date objects contain a Number that represents milliseconds since 1 January 1970 UTC. The Date object is used to work with dates and times.

Constructor

Date() 
// Creates a new Date object.

JavaScript Date Get Method

In javascript, date get methods are used to get the detail from a date object like a year, month, day, hour, minute, second, etc. All the get method was also available in UTC. UTC date methods are used for working with UTC dates (Universal Time Zone dates).

There is also a date set method also but the get method is mostly used. But if you want to know about the date set method then click here.

Check out this also

  1. How to Format Date In JavaScript
  2. Different ways to declare function in javascript

1. The getFullYear() Method

The getFullYear() method returns the year of date in a four-digit number (YYYY).

Example of getFullYear() method

// Sun Oct 11 2020 01:10:20 GMT+0530 (India Standard Time)

let date = new Date();
d.getFullYear(); // output => 2020

2. The getMonth() Method

The getMonth() method returns the month of a date as a number (0-11). As the month value is 1 to 12 but javascript date getMonth() gives one value decremented.

Example of getMonth() method

// Sun Oct 11 2020 01:10:20 GMT+0530 (India Standard Time)

let date = new Date();
d.getMonth(); // output => 9

3. JavaScript getDate() Method

The getDate() method give the day of a date as a number (1-31):

Example of getDate() method

// Sun Oct 11 2020 01:10:20 GMT+0530 (India Standard Time)

let date = new Date();
d.getDate(); // output => 11

4. The getHours() Method

The getHours() method gives the hours of a date as a number (0-23). Which is in 24 hours format.

Example of getHours() Method

// Sun Oct 11 2020 01:10:20 GMT+0530 (India Standard Time)

let date = new Date();
d.getHours(); // output => 1

5. The getMinutes() Method

The getMinutes() method gives the minutes of a date as a number (0-59):

Example of getMinutes() Method

// Sun Oct 11 2020 01:10:20 GMT+0530 (India Standard Time)

let date = new Date();
d.getMinutes(); // output => 10

6. The getSeconds() Method

The getSeconds() method gives the seconds of a date as a number (0-59).

Example of getSeconds() Method

// Sun Oct 11 2020 01:10:20 GMT+0530 (India Standard Time)

let date = new Date();
d.getSeconds(); // output => 20

7. The getMilliseconds() Method

The getMilliseconds() method gives the milliseconds of a date as a number (0-999).

Example of getMilliseconds() Method

// Sun Oct 11 2020 01:10:20 GMT+0530 (India Standard Time)

let date = new Date();
d.getMilliSeconds(); // 157 => Changes every time

8. The getTime() Method

The getTime() method gives the number of milliseconds since January 1, 1970.

Example of getTime() Method

// Sun Oct 11 2020 01:10:20 GMT+0530 (India Standard Time)

let date = new Date();
d.getTime(); // 1602367701904 => Changes every time

9. The getDay() Method

The getDay() method gives the weekday of a date as a number (0-6).

Example of getDay() Method

// Sun Oct 11 2020 01:10:20 GMT+0530 (India Standard Time)

let date = new Date();
d.getDay(); // 0 

10. The Date.now() Method

The Date.now() method gives the time in ECMAScript 5.

Example of Date.now() Method

// Sun Oct 11 2020 01:10:20 GMT+0530 (India Standard Time)

Date.now() // 1602368086002 => Changes every time

I hope you enjoy this tutorial, in this, you get what you need to fetch the required detail from the date object. The date method also helps to format a date in any required format.

Check out our other javascript quiz – 

  1. JS Quiz – does the splice method replace array element
  2. JS Quiz – is the console log function really override
  3. JavaScript Quiz – delete work on object create method object
  4. JavaScript Quiz – null and undefined act as value in function parameter

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link