bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

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

JavaScript Set Date Methods

Concept visual

JavaScript Set Date Methods

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

Start at both ends

Set Date methods let you set date values (years, months, days, hours, minutes, seconds, milliseconds) for a Date Object.

Set Date Methods

Set Date methods are used for setting a part of a date:

Method

Description setDate()

Formula

Set the day as a number (1 - 31)

setFullYear() Set the year (yyyy) setHours()

Formula

Set the hour (0 - 23)

setMilliseconds()

Formula

Set the milliseconds (0 - 999)

setMinutes()

Formula

Set the minutes (0 - 59)

setMonth()

Formula

Set the month (0 - 11)

setSeconds()

Formula

Set the seconds (0 - 59)

setTime() Set the time (milliseconds since January 1, 1970)

The setFullYear() Method

The setFullYear()

method sets the year of a date object. In this example to 2020:

Example

const d = new Date("January 01, 2025");
d.setFullYear(2020);

The setFullYear()

method can optionally set month and day:

Example

const d = new Date("January 01, 2025");
d.setFullYear(2020, 11, 3);

The setMonth() Method

The setMonth()

Formula

method sets the month of a date object (0 - 11):

Example

const d = new Date("January 01, 2025");
d.setMonth(11);

The setDate() Method

The setDate()

Formula

method sets the day of a date object (1 - 31):

Example

const d = new Date("January 01, 2025");
d.setDate(15);

The setDate()

method can also be used to add days to a date:

Example

const d = new Date("January 01, 2025");
d.setDate(d.getDate() + 50);

If adding days shifts the month or year, the changes are handled automatically by the Date object.

The setHours() Method

The setHours()

Formula

method sets the hours of a date object (0 - 23):

Example

const d = new Date("January 01, 2025");
d.setHours(22);

The setHours()

method can also be used to set minutes and seconds.

Example

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

The setMinutes() Method

The setMinutes()

Formula

method sets the minutes of a date object (0 - 59):

Previous

JavaScript Number Reference

Next

JavaScript Array Sort