JavaScript Set Date Method

The JavaScript Date() object helps when working with dates. To create a new object the with current date and time, add the object to your script.

The JavaScript Set Date Methods are used to set date values (years, months, days, hours, minutes, seconds, milliseconds) for a Date Object. Following are some methods mentioned:

JavaScript setDate() method can be used to set a date value to the date object with an integer value between 1 and 31 that represents day of the current month

MethodsExplanation
setFullYear()It is used to set the year
setMonth()It is used to set the month
setDate()It is used to set the day as a number
setTime()It is used to set the time
setHours()It is used to set the hour
setMinutes()It is used to set the minutes
setSeconds()It is used to set the second
setMilliseconds()It is used to set the milliseconds
  • The setFullYear() Method: The setFullYear() method is used to set the year of a date object and it is one of the methods JavaScript Set Date Method.
<!DOCTYPE html>
<html>
<body>

<script>
const y = new Date();
y.setFullYear(2021);
document.write(y);
</script>

</body>
</html> 

Output:

JavaScript Set Date Method

We can set month and day optionally :

<!DOCTYPE html>
<html>
<body>

<script>
const y = new Date();
y.setFullYear(2021, 19, 9);
document.write(y);
</script>

</body>
</html>

Output:

image 127
  • The setMonth() Method: The setMonth() the method is used to set the month of a date object (0-11):
<!DOCTYPE html>
<html>
<body>

<script>
const m = new Date();
m.setMonth(11);
document.write(m);
</script>

</body>
</html>

Output:

image 111
  • The setDate() Method: The setDate() method is used to set the day of a date object (1-31):
<!DOCTYPE html>
<html>
<body>

<script>
const t = new Date();
document.write(t.setDate(19));
</script>

</body>
</html>

Output:

image 153

We can add days to a date using the setDate() method:

<!DOCTYPE html>
<html>
<body>

<script>
const t = new Date();
t.setDate(t.getDate() + 40);
document.write(t);
</script>

</body>
</html>

Output:

image 154
  • The setHours() Method: The setHours() method is used to set the hours of a date object (0-23):
<!DOCTYPE html>
<html>
<body>

<script>
const h = new Date();
document.write(h.setHours(22));
</script>

</body>
</html>

Output:

image 112
  • The setMinutes() Method: The setMinutes() method is used to set the minutes of a date object (0-59):
<!DOCTYPE html>
<html>
<body>

<script>
const m = new Date();
m.setMinutes(21);
document.write(m);
</script>

</body>
</html>

Output:

image 113
  • The setSeconds() Method: The setSeconds() method is used to set the seconds of a date object (0-59):
<!DOCTYPE html>
<html>
<body>

<script>
const s = new Date();
s.setSeconds(22);
document.write(s);
</script>

</body>
</html>

Output:

image 114

JavaScript Set Date Method – Comparison

We can compare the dates easily. The below example compares today’s date with March 18, 2200:

<!DOCTYPE html>
<html>
<body>

<script>
let t = "";
const d = new Date();
const sd = new Date();
sd.setFullYear(2200, 2, 18)

if (sd > d) {
  t = "Today is before March 18, 2200";
} else {
  t = "Today is after March 18, 2200";
}
document.write(t);
</script>

</body>
</html>

Output:

image 115