Skip to main content

JavaScript Date Set Methods

By SamK
0
0 recommends
Category(s)
Topic(s)

Set Date methods allow you to modify date values such as years, months, days, hours, minutes, seconds, and milliseconds within a Date object.

List of Set Date Methods

setDate(): Sets the day of the month (1-31). 
setFullYear(): Sets the year (YYYY).  
setHours(): Sets the hour of the day (0-23). 
setMilliseconds(): Sets the milliseconds (0-999).  
setMinutes(): Sets the minutes (0-59).  
setMonth(): Sets the month (0-11).  
setSeconds(): Sets the seconds (0-59). 
setTime(): Sets the time in milliseconds since Jan 1, 1970. 

The setFullYear() Method

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

Setting the Year

const date = new Date("January 01, 2025");
date.setFullYear(2020);
console.log(date); 
// Outputs: Date Wed Jan 01 2020 00:00:00 ...

Setting Year, Month, and Day

const date = new Date("January 01, 2025");
date.setFullYear(2020, 11, 3);
console.log(date); 
// Outputs: New date with year 2020, month December, and day 3

The setMonth() Method

The setMonth() method sets the month of a Date object (0-11, where 0 represents January and 11 represents December).

const date = new Date("January 01, 2025");
date.setMonth(11);
console.log(date); 
// Outputs: New date with month set to December

The setDate() Method

The setDate() method sets the day of the month.

Setting the Day

const date = new Date("January 01, 2025");
date.setDate(15);
console.log(date); 
// Outputs: New date with day set to 15

Adding Days to a Date

const date = new Date("January 01, 2025");
date.setDate(date.getDate() + 50);
console.log(date); 
// Outputs: A new date, adjusted by 50 days

Note: If the new day exceeds the month's limit, the Date object automatically adjusts the month and year accordingly.

The setHours() Method

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

Setting the Hour

const date = new Date("January 01, 2025");
date.setHours(22);
console.log(date); 
// Outputs: New date with hour set to 22

Setting Hours, Minutes, and Seconds

const date = new Date("January 01, 2025");
date.setHours(22, 10, 20);
console.log(date); 
// Outputs: New date with hour 22, minutes 10, and seconds 20

The setMinutes() Method

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

const date = new Date("January 01, 2025");
date.setMinutes(30);
console.log(date); 
// Outputs: New date with minutes set to 30

The setSeconds() Method

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

const date = new Date("January 01, 2025");
date.setSeconds(30);
console.log(date); 
// Outputs: New date with seconds set to 30

The setTime() Method

The setTime() method sets the time in milliseconds since January 1, 1970.

const date = new Date();
date.setTime(1672531199000);
console.log(date); 
// Outputs: A new date corresponding to the given milliseconds

Comparing Dates

JavaScript allows comparing date objects easily.

Comparing Today’s Date with a Future Date

let message = "";
const today = new Date();
const futureDate = new Date();
futureDate.setFullYear(2100, 0, 14);
if (futureDate > today) {
 message = "Today is before January 14, 2100.";
} else {
 message = "Today is after January 14, 2100.";
}
console.log(message); 
// Outputs: "Today is before January 14, 2100."

Questions & Answers