JavaScript provides several methods to set different components of a Date
object. These methods allow you to modify the year, month, day, hour, minute, second, and millisecond of a date.
1. setFullYear()
Sets the year of the Date
object.
let date = new Date(); date.setFullYear(2030); console.log(date); // Output: New date with the year set to 2030
Syntax:
date.setFullYear(year, month, day);
year
: The year value to set.month
: Optional. The month (0-11) to set.day
: Optional. The day of the month (1-31) to set.
2. setMonth()
Sets the month (0-11) of the Date
object.
date.setMonth(6); // July (0-indexed) console.log(date); // Output: New date with the month set to July
Syntax:
date.setMonth(month, day);
month
: The month value to set (0-11).day
: Optional. The day of the month to set.
3. setDate()
Sets the day of the month (1-31) for a Date
object.
date.setDate(15); console.log(date); // Output: New date with the day set to 15
Syntax:
date.setDate(day);
day
: The day of the month value to set (1-31).
4. setHours()
Sets the hours (0-23) for a Date
object.
date.setHours(22); // Sets time to 10 PM console.log(date); // Output: New date with the hours set to 22
Syntax:
date.setHours(hours, minutes, seconds, milliseconds);
hours
: The hour value to set (0-23).minutes
: Optional. The minutes value to set (0-59).seconds
: Optional. The seconds value to set (0-59).milliseconds
: Optional. The milliseconds value to set (0-999).
5. setMinutes()
Sets the minutes (0-59) for a Date
object.
date.setMinutes(45); console.log(date); // Output: New date with the minutes set to 45
Syntax:
date.setMinutes(minutes, seconds, milliseconds);
minutes
: The minutes value to set (0-59).seconds
: Optional. The seconds value to set.milliseconds
: Optional. The milliseconds value to set.
6. setSeconds()
Sets the seconds (0-59) for a Date
object.
date.setSeconds(30); console.log(date); // Output: New date with the seconds set to 30
Syntax:
date.setSeconds(seconds, milliseconds);
seconds
: The seconds value to set (0-59).milliseconds
: Optional. The milliseconds value to set.
7. setMilliseconds()
Sets the milliseconds (0-999) for a Date
object.
date.setMilliseconds(250); console.log(date); // Output: New date with the milliseconds set to 250
Syntax:
date.setMilliseconds(milliseconds);
milliseconds
: The milliseconds value to set (0-999).
8. setTime()
Sets the Date
object to the time represented by a number of milliseconds since January 1, 1970.
date.setTime(1609459200000); // Sets the date to Jan 1, 2021 console.log(date); // Output: New date corresponding to the set time
Syntax:
date.setTime(milliseconds);
milliseconds
: The number of milliseconds since January 1, 1970.
9. setUTCFullYear()
Sets the full year of the Date
object according to universal time.
date.setUTCFullYear(2025); console.log(date); // Output: New date with the UTC year set to 2025
Syntax:
date.setUTCFullYear(year, month, day);
year
: The year value to set.month
: Optional. The month value to set (0-11).day
: Optional. The day of the month value to set (1-31).
10. setUTCMonth()
Sets the month (0-11) of the Date
object according to universal time.
date.setUTCMonth(11); // December (0-indexed) console.log(date); // Output: New date with the UTC month set to December
Syntax:
date.setUTCMonth(month, day);
month
: The month value to set (0-11).day
: Optional. The day of the month value to set.
11. setUTCDate()
Sets the day of the month (1-31) for a Date
object according to universal time.
date.setUTCDate(10); console.log(date); // Output: New date with the UTC day set to 10
Syntax:
date.setUTCDate(day);
day
: The day of the month value to set (1-31).
12. setUTCHours()
Sets the hours (0-23) for a Date
object according to universal time.
date.setUTCHours(15); // Sets time to 3 PM UTC console.log(date); // Output: New date with the UTC hours set to 15
Syntax:
date.setUTCHours(hours, minutes, seconds, milliseconds);
hours
: The hour value to set (0-23).minutes
: Optional. The minutes value to set.seconds
: Optional. The seconds value to set.milliseconds
: Optional. The milliseconds value to set.
13. setUTCMinutes()
Sets the minutes (0-59) for a Date
object according to universal time.
date.setUTCMinutes(50); console.log(date); // Output: New date with the UTC minutes set to 50
Syntax:
date.setUTCMinutes(minutes, seconds, milliseconds);
minutes
: The minutes value to set (0-59).seconds
: Optional. The seconds value to set.milliseconds
: Optional. The milliseconds value to set.
14. setUTCSeconds()
Sets the seconds (0-59) for a Date
object according to universal time.
date.setUTCSeconds(45); console.log(date); // Output: New date with the UTC seconds set to 45
Syntax:
date.setUTCSeconds(seconds, milliseconds);
seconds
: The seconds value to set (0-59).milliseconds
: Optional. The milliseconds value to set.
15. setUTCMilliseconds()
Sets the milliseconds (0-999) for a Date
object according to universal time.
date.setUTCMilliseconds(123); console.log(date); // Output: New date with the UTC milliseconds set to 123
Syntax:
date.setUTCMilliseconds(milliseconds);
milliseconds
: The milliseconds value to set (0-999).
Summary of JavaScript Date Set Methods
setFullYear()
,setMonth()
,setDate()
: Set year, month, and day.setHours()
,setMinutes()
,setSeconds()
: Set time components.setTime()
: Set date using milliseconds since the Unix Epoch.setUTC*()
methods: Set date and time components in UTC.
These methods provide a powerful way to manipulate dates and times in JavaScript.