8 Javascript date get methods

In this blog post, we're taking a look at 8 javascript date get methods and break them down in a simple way.⁣ ⁣ The methods we will cover:⁣

  • Date.now()⁣
  • getFullYear()⁣
  • getMonth()⁣
  • getDay()⁣
  • getHours()⁣
  • getMinutes()⁣
  • getTime()⁣
  • getDate()⁣

Date Now - Date.now()⁣

Get the time using ECMAScript 5.

Date.now();

The static Date.now() method returns the number of UNIX milliseconds passed since January 1, 1970:00:00:00 UTC

Get Full Year - getFullYear()⁣

Get the year as a four-digit number(yyyy)

const year = new Date().getFullYear();
//2020

Get Month - getMonth()⁣

Get the month as a number (0-11)

const month = new Date().getMonth() + 1;
//11

To get the correct month, you must add 1.

Get Day - getDay()⁣

Get the weekday as a number (0-6)

const day = new Date().getDay();
const days = [
  "Sunday",
  "Monday",
  "Tuesday",
  "Wednesday",
  "Thursday",
  "Friday",
  "Saturday",
];

days[day];
//Friday

Get Hours - getHours()⁣

Get the hour (0-23)

const hours = new Date().getHours();
//8

Get Minutes - getMinutes()⁣

Get the minutes (0-59)

const minutes = new Date().getMinutes();
//40

Get Time - getTime()⁣

Get the time in milliseconds since January 1, 1970

const time = new Date().getTime();
//159.....

The internal clock in javascript counts from midnight January 1, 1970

Get Date - getDate()⁣

Get the day as a number (1-31)

const date = new Date().getDate();
//27

The internal clock in javascript counts from midnight January 1, 1970

Conclusion

Do you use date methods?⁣ I hope this post helps you with the javascript date methods.

💌 If you'd like to receive more tutorials in your inbox, you can sign up for the newsletter here.

Discussions

Up next