. Advertisement .
..3..
. Advertisement .
..4..
If you want to add minutes to a date in JavaScript, you can modify the corresponding Date object with built-in methods. Learn more about them below.
Add Minutes To A Date In JavaScript
Date()
First, we need to create a valid Date object. Date() can be used as either a function or a constructor. The main difference is that when called as a function, it returns a string representing the current time. That is why you need to use the new keyword to invoke it as a constructor.
There are many ways to create a Date object with this constructor:
new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
Remember that Date objects in JavaScript represent a point in time by basically using the number of milliseconds between it and the language’s epoch (1 January 1970 UTC).
The formats of Date objects are platform-independent, meaning you don’t need to adjust your code for each operating system. This isn’t the case with many programming languages.
You can choose how specific you want the date and time to be, up to milliseconds.Date() replaces missing components with the lowest values.
If you provide no parameters, the constructor will create a Date object representing the current date and time. Keep in mind that JavaScript uses the local time to evaluate all the parameters, not UTC.
Example:
const date1 = new Date('2022-09-01 13:15:00');
const date2 = new Date();
console.log(date1);
console.log(date2);
Output:
2022-09-01T06:15:00.000Z
2022-08-29T11:24:08.391Z
We have created two Date objects: one for a specific moment with component values provided by us and one for the current date and time that Date() gets from the system.
You can now use the getMinutes() and setMinutes() methods to add minutes to these Date objects. Our idea is basically like this: we get the value of the minute component of a Date object, add some minutes to it, and set the result back to the object.
getMinutes() And setMinutes()
The Date.getMinutes() method returns the minutes of a date in JavaScript (using local time). It has no parameter, and the returned value is an integer between 0 and 59 that represents the minutes according to the local time of a date.
This is how you can use getMinutes() to get the minute values of the date1 and date1 objects in our example:
const min1 = date1.getMinutes();
const min2 = date2.getMinutes();
console.log(min1);
console.log(min2);
Output:
15
24
The Date.getMinutes() method sets the minute component of a Date object to a value we provide. Its full syntax is as follows:
setMinutes(minutesValue, secondsValue, msValue)
Parameters:
- minutesValue: an integer number between 0 and 59 that represents the minute value you want to set to the Date object.
- secondsValue: an integer number between 0 and 59 that represents the second value you want to set to the Date object.
- msValue: an integer number between 0 and 999 that represents the millisecond value you want to set to the Date object.
Both secondsValue and msValue are optional, and there is no need for us to use them in our example.
The method will update the Date object using your provided parameters. But what it returns is the milliseconds between the epoch and the updated time.
The following example demonstrates how you can use the setMinutes() method together with getMinutes() to add minutes to a Date object:
const new_ms1 = date1.setMinutes(min1 + 30);
console.log(date1);
console.log(new_ms1);
const new_ms2 = date2.setMinutes(min2 - 45);
console.log(date2);
console.log(new_ms2);
Output:
2022-09-01T06:45:00.000Z
1662014700000
2022-08-29T10:39:08.391Z
1661769548391
getUTCMinutes() And setUTCMinutes()
These methods work like getMinutes() and setMinutes(), but they use universal time instead of local time.
Example:
const today = new Date();
const min = today.getUTCMinutes();
console.log(today);
const new_ms = today.setUTCMinutes(min + 30);
console.log(today);
Output:
2022-08-29T11:43:18.372Z
2022-08-29T12:13:18.372Z
Summary
You can add minutes to a date in JavaScript by getting the minute value of a Date object first and setting it later after adding some value to it. You can use either local time with getMinutes() and setMinutes(), or universal time with getUTCMinutes() and setUTCMinutes().We have also written a follow-up guide on adding hours to date, so don’t forget to check it out.
Leave a comment