JavaScript Adding minutes to Date object
Last Updated :
20 Jun, 2023
Given a date and the task is to add minutes to it using JavaScript. To add minutes to the date object, some methods are used which are listed below:
JavaScript getMinutes() Method: This method returns the Minutes (from 0 to 59) of the provided date and time.
Syntax:
Date.getMinutes()
Parameters: This method does not accept any parameters.
Return value: It returns a number, from 0 to 59, representing the minutes.
JavaScript setMinutes() Method: This method set the minutes of a date object. This method can also be used to set the seconds and milliseconds.
Syntax:
Date.setMinutes(min, sec, millisec)
Parameters:
- min: It is required parameter. It specifies the integer representing the minutes. Values expected are 0-59, but other values are allowed.
- sec: It is optional parameter. It specifies the integer representing the seconds. Values expected are 0-59, but other values are allowed.
- millisec: It is optional parameter. It specifies the integer representing the milliseconds. Values expected are 0-999, but other values are allowed.
min = -1, it means the last minute of the previous hour and same for the other parameters.
If min value passed to 60, it means the first minute of the next hour and same for the other parameters.
Return Value: It returns the new date with updated minute which is set by setMinutes() method.
JavaScript getTime() method: This method returns the number of milliseconds between midnight of January 1, 1970, and the specified date.
Syntax:
Date.getTime()
Parameters: This method does not accept any parameters.
Return value: It returns a number, representing the number of milliseconds since midnight January 1, 1970.
JavaScript setTime() method: This method set the date and time by adding/subtracting a defined number of milliseconds to/from midnight January 1, 1970.
Syntax:
Date.setTime(millisec)
Parameters:
- millisec: Number of milliseconds to add/subtract to the given date
Return value: It returns, representing the number of milliseconds between the date object and midnight January 1 1970.
Example 1: This example added 4 minutes to the variable today by using setTime() and getTime() methods.
JavaScript
let today = new Date();
console.log("Date = " + today);
Date.prototype.addMins = function (m) {
this.setTime(this.getTime() + (m * 60 * 1000));
return this;
}
let a = new Date();
a.addMins(4);
console.log(a);
Output
Date = Tue Jun 13 2023 20:16:29 GMT+0530 (India Standard Time)
Date Tue Jun 13 2023 20:20:29 GMT+0530 (India Standard Time)
Example 2: This example adds 6 minutes to the variable today by using setMinutes() and getMinutes() methods.
JavaScript
let today = new Date();
console.log("Date = " + today);
Date.prototype.addMinutes = function (m) {
this.setMinutes(this.getMinutes() + m);
return this;
}
let a = new Date();
a.addMinutes(6);
console.log(a);
Output:
Date = Tue Jun 13 2023 20:18:07 GMT+0530 (India Standard Time)
Date Tue Jun 13 2023 20:24:07 GMT+0530 (India Standard Time)
Similar Reads
JavaScript Adding seconds to Date object Given a date object and the task is to add seconds to the date using JavaScript. We will use a few methods to add seconds to the date, that are discussed below: JavaScript getSeconds() Method: This method returns the Seconds(from 0 to 59) of the provided date and time. Syntax: Date.getSeconds() Para
3 min read
JavaScript Adding days in milliseconds to Date object Given a date, the task is to add days in milliseconds to it using JavaScript. To add days in milliseconds to date objects in JavaScript, some methods are used which are listed below: JavaScript getMilliseconds() Method: This method returns the milliseconds (from 0 to 999) of the provided date and ti
2 min read
JavaScript Adding hours to the Date object Given a date, the task is to add hours to it. To add hours to date in javascript, we're going to discuss a few techniques. First few methods to know. JavaScript getHours() Method: This method returns the hour (from 0 to 23) of the provided date and time. Syntax: Date.getHours() Parameters: This meth
3 min read
JavaScript Set Date Methods There are various methods to set the date in JavaScript. The data values can be set like years, months, days, hours, minutes, seconds, and milliseconds for a Date Object. Method: setDate(): It is used to set the day as a number (1-31).setFullYear(): It is used to set the year (optionally month and d
2 min read
How to Add Days to Date in JavaScript? Adding days to a date in JavaScript is a common task when working with dates and times. This involves manipulating the Date object to calculate a future or past date by adding a specific number of days. This is useful for tasks like scheduling events, and deadlines, or simply adjusting dates dynamic
2 min read
JavaScript Date UTC() Method In JavaScript, the Date.UTC() method is used to create a date object representing a specified date and time in UTC (Coordinated Universal Time). It accepts the year, month, day, hour, minute, second, and millisecond components of the date and returns the number of milliseconds since January 1, 1970,
4 min read