JavaScript Fundamentals / Date and Times

Get/Set Date & Time

⚡️Retrieving Date Components

You can retrieve various parts of a date using the **get method** in the following ways :

Method

Description

getFullYear()

Get year as a four digit number (yyyy)

getMonth()

Get month as a number (0-11)

getDate()

Get day as a number (1-31)

getDay()

Get weekday as a number (0-6)

getHours()

Get hour (0-23)

getMinutes()

Get minute (0-59)

getSeconds()

Get second (0-59)

getMilliseconds()

Get millisecond (0-999)

getTime()

Get time (milliseconds since January 1, 1970)

📝The get methods above return Local time.

Note: These methods can also be used with new Date() to get the current time.

✅ getFullYear() Method

The getFullYear() method returns the year of a date as a four digit number:

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

💡getMonth() Method

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

const d = new Date("2025-06-12");
d.getMonth();  //6

💠 getDate() Method

The getDate() method returns the day of a date as a number (1-31).

const d = new Date("2021-03-25");
d.getDate();  //25

📍 getHours() Method

The getHours() method returns the hours of a date as a number (0-23).

Example :

const d = new Date("2021-03-25");
d.getHours();  //5

✒️ getMinutes() Method

The getMinutes() method returns the minutes of a date as a number (0-59).

Example :

const d = new Date("2022-03-31");
d.getMinutes();  //30

🏷️ getSeconds() Method

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

Example :

const d = new Date("2021-03-25");
d.getSeconds();  //30

▶️ getMilliseconds() Method

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

Example :

const d = new Date("2021-03-25");
d.getMilliseconds();  //0

📚 getDay() Method

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

In JavaScript, the first day of the week (day 0) is Sunday.

Example :

const d = new Date("2024-06-10");
d.getDay();  //1

📝Note

You can use an array of names, and getDay() to return weekday as a name:

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

❇️ getTime() Method

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

Example :

const d = new Date("1978-01-01");
d.getTime();  //252460800000

✔️ Manipulating Dates

You can modify various parts of a date using the set method in the following ways:

Method

Description

setDate()

Set the day as a number (1-31)

setFullYear()

Set the year (yyyy)

setHours()

Set the hour (0-23)

setMilliseconds()

Set the milliseconds (0-999)

setMinutes()

Set the minutes (0-59)

setMonth()

Set the month (0-11)

setSeconds()

Set the seconds (0-59)

setTime()

Set the time (milliseconds since January 1, 1970)

Note: These methods can also be used with new Date() to get current time.

🧩 setFullYear() Method

The setFullYear() method sets the year of a date object.

Example :

const d = new Date("Jan 01, 2025");
d.setFullYear(2029); // Jan 01 2029
  • setFullYear() method can optionally set month and day.

Example :

const d = new Date("Jan 01, 2025");
d.setFullYear(2020, 11, 3); // Dec 03 2020

🔅 setMonth() Method

The setMonth() method sets the month of a date object (0-11).

Example :

const d = new Date("Jan 01, 2025");
d.setMonth(11); //Dec 01 2025 

🔰 setDate() Method

The setDate() method sets the day of a date object (1-31).

Example :

const d = new Date("May 01, 2025");
d.setDate(15); // May 15 2025 
  • setDate() method can also be used to add days to a date.

Example :

const d = new Date("Jan 01, 2025");
d.setDate(d.getDate() + 50);  //Feb 20 2025

🔴 setHours() Method

The setHours() method sets the hours of a date object (0-23).

Example :

const d = new Date("Jan 01, 2025");
d.setHours(22);  //Jan 01 2025 22:00:00
  • setHours() method can also be used to set minutes and seconds.

Example :

const d = new Date("Jan 01, 2025");
d.setHours(22, 10, 20); //Jan 01 2025 22:10:20 

🖇️ setMinutes() Method

The setMinutes() method sets the minutes of a date object (0-59).

Example :

const d = new Date("Jan 01, 2025");
d.setMinutes(30);  //Jan 01 2025 00:30:00 

♦️ setSeconds() Method

The setSeconds() method sets the seconds of a date object (0-59).

Example :

const d = new Date("Jan 01, 2025");
d.setSeconds(30);  //Jan 01 2025 00:00:30