JavaScript Set Date Methods

By | August 24, 2022

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:

MethodDescription
setDate()Set the day as a number (1-31)
setFullYear()Set the year (optionally month and day)
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)

The setFullYear() Method

The setFullYear() method sets the year of a date object. In this example to 2020:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setFullYear()</h2>
<p>The setFullYear() method sets the year of a date object:</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setFullYear(2020);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript setFullYear()

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

Mon Aug 24 2020 14:04:51 GMT-0700 (Pacific Daylight Time)

The setFullYear() method can optionally set month and day:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setFullYear()</h2>
<p>The setFullYear() method can optionally set month and day.</p>
<p>Please note that month counts from 0. December is month 11:</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setFullYear(2020, 11, 3);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript setFullYear()

The setFullYear() method can optionally set month and day.

Please note that month counts from 0. December is month 11:

Thu Dec 03 2020 14:06:19 GMT-0800 (Pacific Standard Time)

The setMonth() Method

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

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setMonth()</h2>
<p>The setMonth() method sets the mont of a date object.</p>
<p>Note that months count from 0. December is month 11:</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setMonth(11);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript setMonth()

The setMonth() method sets the mont of a date object.

Note that months count from 0. December is month 11:

Sat Dec 24 2022 14:07:08 GMT-0800 (Pacific Standard Time)

The setDate() Method

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

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setDate()</h2>
<p>The setDate() method sets the day of a date object:</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setDate(15);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript setDate()

The setDate() method sets the day of a date object:

Mon Aug 15 2022 14:07:55 GMT-0700 (Pacific Daylight Time)

The setDate() method can also be used to add days to a date:

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setDate()</h2>
<p>The setDate() method can be used to add days to a date.</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setDate(d.getDate() + 50);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript setDate()

The setDate() method can be used to add days to a date.

Thu Oct 13 2022 14:08:48 GMT-0700 (Pacific Daylight Time)

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

The setHours() Method

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

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setHours()</h2>
<p>The setHours() method sets the hours of a date object:</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setHours(22);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>​

Result:

JavaScript setHours()

The setHours() method sets the hours of a date object:

Wed Aug 24 2022 22:09:40 GMT-0700 (Pacific Daylight Time)

The setMinutes() Method

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

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setMinutes()</h2>
<p>The setMinutes() method sets the minutes of a date object (0-59):</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setMinutes(30);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript setMinutes()

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

Wed Aug 24 2022 14:30:37 GMT-0700 (Pacific Daylight Time)

The setSeconds() Method

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

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript setSeconds()</h2>
<p>The setSeconds() method sets the seconds of a date object (0-59):</p>
<p id="demo"></p>
<script>
const d = new Date();
d.setSeconds(30);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>

Result:

JavaScript setSeconds()

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

Wed Aug 24 2022 14:16:30 GMT-0700 (Pacific Daylight Time)

Compare Dates

Dates can easily be compared.

The following example compares today’s date with January 14, 2100:

Example

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
let text;
const today = new Date();
const someday = new Date();
someday.setFullYear(2100, 0, 14);
if (someday > today) {
  text = "Today is before January 14, 2100.";
} else {
  text = "Today is after January 14, 2100.";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>

Result:

Today is before January 14, 2100.

Leave a Reply

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