JavaScript also provides several methods to set the different parts of a date object.
The setFullYear()
Method
The setFullYear()
method sets the year of a date object.
Syntax:
dateObj.setFullYear(year, month, day)
Example:
const today = new Date();
today.setFullYear(2024);
console.log(today); // Output: Fri Apr 02 2024 16:41:52 GMT+0000 (Coordinated Universal Time)
The setMonth()
Method
The setMonth()
method sets the month of a date object.
Syntax:
dateObj.setMonth(month, day)
Example:
const today = new Date();
today.setMonth(11);
console.log(today); // Output: Wed Dec 02 2022 16:41:52 GMT+0000 (Coordinated Universal Ti
The setDate()
Method
The setDate()
method sets the day of the month of a date object.
Syntax:
dateObj.setDate(day)
Example:
const today = new Date();
today.setDate(15);
console.log(today); // Output: Thu Apr 15 2021 16:41:52 GMT+0000 (Coordinated Universal Time)
The setHours() Method The setHours() method sets the hour of a date object. Syntax:
dateObj.setHours(hour, min, sec, ms)
Example:
const today = new Date();
today.setHours(13);
console.log(today); // Output: Fri Apr 02 2021 13:41:52 GMT+0000 (Coordinated Universal Time)
The setMinutes() Method The setMinutes() method sets the minutes of a date object. Syntax:
dateObj.setMinutes(min, sec, ms)
Example:
const today = new Date();
today.setMinutes(30);
console.log(today); // Output: Fri Apr 02 2021 16:30:52 GMT+0000 (Coordinated Universal Time)
The setSeconds()
Method
The setSeconds()
method sets the seconds of a date object.
Syntax:
dateObj.setSeconds(sec, ms)
Example:
const today = new Date();
today.setSeconds(45);
console.log(today); // Output: Fri Apr 02 2021 16:41:45 GMT+0000 (Coordinated Universal Time)
The setMilliseconds() Method The setMilliseconds() method sets the milliseconds of a date object. Syntax:
dateObj.setMilliseconds(ms)
Example:
const today = new Date();
today.setMilliseconds(500);
console.log(today); // Output: Fri Apr 02 2021 16:41:52 GMT+0000 (Coordinated Universal Time)
Now that we have learned about the different methods to get and set the different parts of a date object in JavaScript, let's look at a practical example of how we can use these methods to perform common date and time operations in JavaScript. Practical Example: Creating a Countdown Timer A countdown timer is a common feature found on many websites and applications. In this section, we will create a simple countdown timer using JavaScript that counts down from a specified date to the current date and time. Here's the code to create a countdown timer:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Countdown Timer</title>
</head>
<body>
<h1 id="countdown"></h1
JavaScript Set Date Methods
JavaScript provides several methods to set the values of a date object. These methods are used to set the year, month, day, hour, minute, second, and millisecond of a date object.
The setFullYear()
Method
The setFullYear()
method is used to set the year of a date object. It takes one or more arguments: the year and, optionally, the month and day. If only the year is specified, the month and day will be set to their default values (January 1st).
let d = new Date();
d.setFullYear(2022);
console.log(d);
// Output: Sun Jan 01 2022 17:16:55 GMT+0800 (China Standard Time)
d.setFullYear(2023, 11, 31);
console.log(d);
// Output: Mon Dec 31 2023 17:16:55 GMT+0800 (China Standard Time)
The setMonth()
Method
The setMonth()
method is used to set the month of a date object. It takes one or two arguments: the month (0-11) and, optionally, the day. If only the month is specified, the day will be set to its default value (the first day of the month).
let d = new Date();
d.setMonth(3);
console.log(d);
// Output: Mon Apr 02 2023 17:16:55 GMT+0800 (China Standard Time)
d.setMonth(11, 25);
console.log(d);
// Output: Sat Dec 25 2023 17:16:55 GMT+0800 (China Standard Time)
The setDate()
Method
The setDate()
method is used to set the day of the month of a date object. It takes one argument: the day of the month (1-31).
let d = new Date();
d.setDate(15);
console.log(d);
// Output: Sat Apr 15 2023 17:16:55 GMT+0800 (China Standard Time)
The setHours()
Method
The setHours()
method is used to set the hour of a date object. It takes one to four arguments: the hour (0-23), the minute (0-59), the second (0-59), and the millisecond (0-999).
let d = new Date();
d.setHours(15);
console.log(d);
// Output: Mon Apr 03 2023 15:16:55 GMT+0800 (China Standard Time)
d.setHours(15, 30);
console.log(d);
// Output: Mon Apr 03 2023 15:30:55 GMT+0800 (China Standard Time)
d.setHours(15, 30, 45);
console.log(d);
// Output: Mon Apr 03 2023 15:30:45 GMT+0800 (China Standard Time)
d.setHours(15, 30, 45, 500);
console.log(d);
// Output: Mon Apr 03 2023 15:30:45 GMT+0800 (China Standard Time)
The setMinutes()
Method
The setMinutes()
method is used to set the minute of a date object. It takes one or two arguments: the minute (0-59) and, optionally, the second (0-59) and millisecond (0-999).
let d = new Date();
d.setMinutes(30);
console.log(d);
// Output: Mon Apr 03
Setting the Year
The setFullYear()
method allows you to set the year of a date object. It takes in a four-digit year value as a parameter and returns the new date object with the updated year value.
const currentDate = new Date();
currentDate.setFullYear(2023); // sets the year to 2023
Setting the Month
The setMonth()
method allows you to set the month of a date object. It takes in a numeric value representing the month, with January being 0 and December being 11, as a parameter and returns the new date object with the updated month value.
const currentDate = new Date();
currentDate.setMonth(11); // sets the month to December
Setting the Day
The setDate()
method allows you to set the day of a date object. It takes in a numeric value representing the day of the month, with values ranging from 1 to 31, as a parameter and returns the new date object with the updated day value.
const currentDate = new Date();
currentDate.setDate(25); // sets the day to the 25th
Setting the Time
The setHours()
, setMinutes()
, setSeconds()
, and setMilliseconds()
methods allow you to set the time of a date object. They take in numeric values representing the hour, minute, second, and millisecond values, respectively, as parameters and return the new date object with the updated time values.
const currentDate = new Date();
currentDate.setHours(14); // sets the hour to 2:00pm
currentDate.setMinutes(30); // sets the minute to 30
currentDate.setSeconds(0); // sets the second to 0
currentDate.setMilliseconds(0); // sets the millisecond to 0
Combining Set Methods
You can also combine these set methods to update multiple values of a date object at once.
const currentDate = new Date();
currentDate.setFullYear(2023);
currentDate.setMonth(11);
currentDate.setDate(25);
currentDate.setHours(14);
currentDate.setMinutes(30);
currentDate.setSeconds(0);
currentDate.setMilliseconds(0);
By using the JavaScript set date methods, you can easily set specific values for the year, month, day, and time of a date object to create custom dates for your application.