Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do I subtract minutes from a date in JavaScript?
In this tutorial, we will learn to subtract minutes from a date in JavaScript. Working with dates is a common requirement in web development, and JavaScript provides built-in Date class methods to handle date manipulations effectively.
We'll explore three approaches: using native JavaScript methods getMinutes() and setMinutes(), working with milliseconds, and using the Moment.js library for more convenient date operations.
Using the getMinutes() and setMinutes() Methods
This approach uses the Date object's built-in methods to extract current minutes, subtract the desired amount, and update the date object.
Syntax
let date = new Date(set_date); let difference = date.getMinutes() - minutes_to_subtract; date.setMinutes(difference);
Parameters
set_date ? Optional parameter to initialize the Date object with a specific date. If omitted, uses current date and time.
minutes_to_subtract ? Number of minutes to subtract from the date.
Example
<html>
<head>
<title>Subtract Minutes from Date</title>
</head>
<body>
<h3>Using getMinutes() and setMinutes() methods</h3>
<p>Original date: July 31, 2022 11:30:25</p>
<p>After subtracting 132 minutes:</p>
<div id="date1"></div>
<p>Current time minus 40 minutes:</p>
<div id="date2"></div>
<script>
// Subtract 132 minutes from July 31, 2022 11:30:25
let newDate = new Date('July 31, 2022 11:30:25');
let difference = newDate.getMinutes() - 132;
newDate.setMinutes(difference);
document.getElementById("date1").innerHTML = newDate;
// Subtract 40 minutes from current time
let currentDate = new Date();
let currentDifference = currentDate.getMinutes() - 40;
currentDate.setMinutes(currentDifference);
document.getElementById("date2").innerHTML = currentDate;
</script>
</body>
</html>
Using Milliseconds with getTime() Method
This approach converts the date to milliseconds, subtracts the required minutes (converted to milliseconds), and creates a new Date object from the result.
Syntax
let date = new Date(); let totalMilliSeconds = date.getTime(); let millisecondsToSubtract = minutes * 60 * 1000; let newDate = new Date(totalMilliSeconds - millisecondsToSubtract);
Parameters
minutes ? Number of minutes to subtract from the date.
Conversion formula ? minutes × 60 seconds × 1000 milliseconds = total milliseconds to subtract.
Example
<html>
<head>
<title>Subtract Minutes using Milliseconds</title>
</head>
<body>
<h3>Subtract Minutes using getTime() method</h3>
<h4>Subtracting 20 minutes from 2002-05-23T05:30:40:</h4>
<div id="date1"></div>
<script>
// Subtract 20 minutes from specified date
let oldDate = new Date('2002-05-23T05:30:40');
let totalMilliSeconds = oldDate.getTime();
let minutes = 20;
let millisecondsToSubtract = minutes * 60 * 1000;
let newDate = new Date(totalMilliSeconds - millisecondsToSubtract);
document.getElementById("date1").innerHTML = newDate;
</script>
</body>
</html>
Using Moment.js Library
Moment.js provides a more intuitive API for date manipulation. The subtract() method makes it easy to subtract any time unit from a date object.
Syntax
let newDate = moment().subtract(number_of_minutes, 'm').format();
Parameters
number_of_minutes ? Number of minutes to subtract from the date.
'm' ? Flag indicating minutes. Other options include 'y' (years), 'M' (months), 'd' (days), 'h' (hours), 's' (seconds).
Example
<html>
<head>
<title>Subtract Minutes with Moment.js</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment-with-locales.min.js"></script>
</head>
<body>
<h4>Subtracting 100 minutes from current time using Moment.js:</h4>
<div id="momentOutput"></div>
<script>
// Subtract 100 minutes from current date using Moment.js
let newDate = moment().subtract(100, 'm').format();
document.getElementById("momentOutput").innerHTML = newDate;
</script>
</body>
</html>
Comparison of Methods
| Method | Complexity | External Dependency | Best For |
|---|---|---|---|
| getMinutes()/setMinutes() | Medium | None | Simple minute operations |
| Milliseconds approach | Low | None | Precise calculations |
| Moment.js | Low | Moment.js library | Complex date operations |
Conclusion
All three methods effectively subtract minutes from JavaScript dates. The milliseconds approach offers precision and simplicity, while Moment.js provides the most readable code for complex date manipulations. Choose based on your project requirements and whether you want to include external libraries.
