We will explain how to add and subtract dates and times using JavaScript .
How can I add or subtract dates and times?
Use Date objects to add or subtract dates and times.
Add and subtract date and time with Date object
Use Date objects to add or subtract dates and times .
You can add or subtract dates and months by using the setDate and setMonth methods . Also, by calling the setDate method and setMonth method on an instance of the Date object, you can rewrite the data in that instance.
// Get present date and time
let date = new Date();
// Get the date and time one day later Add time
date.setDate(date.getDate() + 1);
console.log(date);
// Get the date and time one week ago Subtract time
date.setDate(date.getDate() - 7);
console.log(date);
// Get the date and time one month later
date.setMonth(date.getMonth() + 1);
console.log(date);
The above sample uses the setDate method to add one day to the current date and time. Use the getDate() method to get the current date, then add 1 and pass it to the setDate method.
Also, use the setMonth method to add one month to the current date and time. Use the getMonth() method to get the current month, then add 1 and pass it to the setMonth method.
Sample of adding and subtracting time to specified date and time
The following is a sample that adds the specified time to the specified date and time.
const date = new Date('2022-03-01T10:00:00');
const hoursToAdd = 3;
date.setHours(date.getHours() + hoursToAdd);
The following is a sample that subtracts a specified time from a specified date and time.
const date = new Date('2022-03-01T10:00:00');
const hoursToSubtract = 3;
date.setHours(date.getHours() - hoursToSubtract);
Summary of Date object methods
We have summarized the explanations of the setHours, setMinutes, setSeconds, and setMilliseconds methods of the Date object in a table.
method | explanation |
---|---|
setHours(hoursValue, minutesValue?, secondsValue?, millisecondsValue?) | Sets the time of a Date object to the specified hours, minutes, seconds, and milliseconds. The arguments hoursValue (hours) and minutesValue (minutes) are required, and secondsValue (seconds) and millisecondsValue (milliseconds) are optional. |
setMinutes(minutesValue, secondsValue?, millisecondsValue?) | Sets the time of a Date object to the specified minutes, seconds, and milliseconds. As for the arguments, minutesValue (minutes) is required, secondsValue (seconds), and millisecondsValue (milliseconds) are optional. |
setSeconds(secondsValue, millisecondsValue?) | Sets the time in a Date object to the specified seconds and milliseconds. The arguments secondsValue (seconds) are required and millisecondsValue (milliseconds) is optional. |
setMilliseconds(millisecondsValue) | Sets the time in a Date object to the specified milliseconds. The argument millisecondsValue (milliseconds) is required. |
We have summarized the explanations of the setFullYear, setMonth, and setDate methods of the Date object in a table.
method | explanation |
---|---|
setFullYear(yearValue, monthValue?, dayValue?) | Sets the year, month, and day of a Date object to the specified year, month, and day. As for the arguments, yearValue (year) is required, monthValue (month), and dayValue (day) are optional. |
setMonth(monthValue, dayValue?) | Sets the month and day of the Date object to the specified month and day. As for the arguments, monthValue (month) is required and dayValue (day) is optional. |
setDate(dayValue) | Sets the date of the Date object to the specified day. The argument dayValue (day) is required. |
How to get the end of the month
To get the end of the month, use the setDate method of the Date object. The setDate method is a method for adding and subtracting dates, but by specifying 0 as an argument, you can get the last day of the month.
// Get present date and time
let date = new Date();
// Get current month end
date.setDate(0);
console.log(date);
// Get the end of the month after 1 month
date.setMonth(date.getMonth() + 1);
date.setDate(0);
console.log(date);
In the sample above, you can get the last day of the month by specifying 0 to the setDate method.
You can also use the setMonth method to add one month to the current date and time, and then use setDate(0) to get the end of the month.
How to calculate leap year
As a general rule, years that are divisible by 4 are considered leap years, but as exceptions, years that are divisible by 100 are considered normal years, and years that are divisible by 400 are considered leap years.
Below is how to calculate leap years using JavaScript.
function isLeapYear(year) {
if (year % 4 !== 0) {
return false; // If it is not divisible by 4, it is not a leap year
} else if (year % 100 !== 0) {
return true; // Leap year if divisible by 4 and not divisible by 100
} else if (year % 400 !== 0) {
return false; // If it is divisible by 4 and also divisible by 100, it is not a leap year
} else {
return true; // Leap year if divisible by 4 and also divisible by 100 and also divisible by 400
}
}
This function determines whether the year passed as an argument is a leap year and returns true or false.
Also, since the year can be obtained from the Date object using the getFullYear method, you can also determine whether the current year is a leap year as follows.
const now = new Date();
const year = now.getFullYear();
console.log(isLeapYear(year)); // trueまたはfalse
Summary
We explained how to add and subtract dates and times .
- Use the Date object to add or subtract dates and times.
- You can add or subtract dates using the setDate method of the Date object.
- By specifying 0 to the setDate method, you can obtain the last day of the month.
It’s convenient to be able to get the end of the month using the Date object!
By using the Date object, you can meet various needs for handling dates and times.
Comments