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)
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.
const date1 = new Date('2022-09-01 13:15:00'); const date2 = new Date(); console.log(date1); console.log(date2);
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()
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);
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)
- 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);
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.
const today = new Date(); const min = today.getUTCMinutes(); console.log(today); const new_ms = today.setUTCMinutes(min + 30); console.log(today);