bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/JavaScript/Working with Data
JavaScript•Working with Data

JavaScript Get Date Methods

Concept visual

JavaScript Get Date Methods

Pointer walk
two pointers
leftright102132436485116
left=0
right=6
1
3

Start at both ends

The new Date() Constructor

In JavaScript, date objects are created with new Date(). new Date() returns a date object with the current date and time.

Get the Current Time const date = new Date();

Date Get Methods

Method

Description getFullYear()

Get year as a four digit number (yyyy) getMonth()

Formula

Get month as a number (0 - 11)

getDate()

Formula

Get day as a number (1 - 31)

getDay()

Formula

Get weekday as a number (0 - 6)

getHours()

Get hour

Formula

(0 - 23)

getMinutes()

Get minute

Formula

(0 - 59)

getSeconds()

Get second

Formula

(0 - 59)

getMilliseconds()

Get millisecond

Formula

(0 - 999)

getTime()

Get time

(milliseconds since January 1, 1970)

The get methods above return

Local time.

Universal time

(UTC) is documented at the bottom of this page.

The get methods return information from existing date objects.
In a date object, the time is static. The "clock" is not "running".

The time in a date object is NOT the same as current time.

The getFullYear() Method

The getFullYear()

method returns the year of a date as a four digit number:

Examples const d = new Date("2021-03-25");
d.getFullYear();
const d = new Date();
d.getFullYear();

Warning !

Formula

Old JavaScript code might use the non - standard method getYear().
getYear() is supposed to return a 2-digit year.
getYear() is deprecated. Do not use it!

The getMonth() Method

The getMonth()

Formula

method returns the month of a date as a number (0 - 11).

In JavaScript, January is month number 0, February is number 1, ... Finally, December is month number 11.

Examples const d = new Date("2021-03-25");
d.getMonth();
const d = new Date();
d.getMonth();

You can use an array of names to return the month as a name:

Examples const months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];
const d = new Date("2021-03-25");
let month = months[d.getMonth()];
const months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];
const d = new Date();
let month = months[d.getMonth()];

The getDate() Method

The getDate()

Formula

method returns the day of a date as a number (1 - 31):
Examples const d = new Date("2021-03-25");
d.getDate();
const d = new Date();
d.getDate();

The getHours() Method

The getHours()

Formula

method returns the hours of a date as a number (0 - 23):
Examples const d = new Date("2021-03-25");
d.getHours();
const d = new Date();
d.getHours();

The getMinutes() Method

The getMinutes()

Formula

method returns the minutes of a date as a number (0 - 59):
Examples const d = new Date("2021-03-25");
d.getMinutes();
const d = new Date();
d.getMinutes();

The getSeconds() Method

The getSeconds()

Formula

method returns the seconds of a date as a number (0 - 59):
Examples const d = new Date("2021-03-25");
d.getSeconds();
const d = new Date();
d.getSeconds();

The getMilliseconds() Method

Previous

JavaScript Number Properties

Next

JavaScript Array Search