How do I subtract minutes from a date in JavaScript?

Front End TechnologyJavascriptWeb Development

In this tutorial, we will learn to subtract minutes from the date in JavaScript. While developing the applications, programmers need to play with the dates many times. All JavaScript programmers are lucky enough that JavaScript contains the built-in date class with hundreds of different methods.

In this tutorial, we will learn to subtract the minutes from the date in vanilla JavaScript using the date object and the Moment JS library.

Using the getMinutes() and setMinutes() Methods

  • In this approach, we will create a new date object and extract the minutes from that using the getMinutes() method.

  • After that, we will update the minutes by subtracting the minutes from the minutes we got from the date.

  • In the next step, we will set the new minutes to the date object using the setMinutes() method of the date class.

Users can follow the below syntax to use the getMinutes( ) and setMinutes( ) method.

Syntax

let date = new Date(set_date); // create new object of the date class.
let difference = date.getMinutes() – minutes_to_substract; // find difference between minutes date.setMinute( difference ); // set new minutes

Parameters

  • set_date − It is an optional parameter for the object of date class. As this parameter you can pass a date and time to initialize date object with it, otherwise it initializes the date object with the current date and time.

Example

The below example demonstrates the above approach.

<html>
<head>
   <title> Subtract the minutes from the date </title>
</head>
<body>
   <h3>The getMinutes() and setMinutes() methods in JavaScript.</h3>
   <p>Subtract 132 minutes from the July 31, 2022 11:30:25 :</p>
   <div id="date1"> </div>
   <p>Subtract the 40 minutes from the current time.</p>
   <div id="date2"> </div>
   <script type="text/javascript">
      let date1 = document.getElementById("date1");
      let date2 = document.getElementById("date2");
      
      // substracting the 132 minutes from the July 31, 2022 11:30:25
      let newdate = new Date('July 31, 2022 11:30:25');
      let difference = newdate.getMinutes() - 132;
      newdate.setMinutes(difference);
      date1.innerHTML = newdate;

      // subtracting the 40 minutes from the current time
      newdate = new Date();
      difference = newdate.getMinutes() - 40;
      newdate.setMinutes(difference);
      date2.innerHTML = newdate;
   </script>
</body>
</html>

In the second output, users can see that they will get date and time after subtracting the 40 minutes from the current time.

Subtract the Minutes from the Date in the Millisecond Format

In this approach

  • First we will use the getTime() method to get the total milliseconds of the date.

  • After that, we will subtract the minutes from the total milliseconds in the form of milliseconds.

  • Users can multiply the minutes by 60 seconds * 1000 milliseconds to convert the minutes tomilliseconds.


Syntax

Below, users can see the syntax to use this approach.

let date = new Date( );
let totalMilliSeconds = date.getTime( );
let millisecondsToSubtract = minutes * 60 * 1000;
let newDate = new Date ( totalMilliSeconds – millisecondsToSubtract )

Parameters

  • minutes − Users need to replace minutes with the numeric value. It is the total minutes which users wants to subtract from the date.

Example

The below example demonstrate to subtract the minutes from the date in the form of milliseconds.

<html>
<head>
   <title> Subtract the minutes from the date </title>
</head>
<body>
   <h3> Subtract the Minutes from the date using the getTime() method. </h3>
   <h4> Subtracting the 20 minutes from the 2002-05-23T05:30:40 :</h4>
   <div id="date1"> </div>
   <script type="text/javascript">
      let date1 = document.getElementById("date1");

      // subtracting the 20 minutes from the 2021-02-23T09:33:10
      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)
      date1.innerHTML = newDate;
   </script>
</body>
</html>

In the above example, we have successfully subtract the 20 minutes in the form of milliseconds from the oldDate object and we get the above output.

Using the Moment JS library

The Moment JS is the special library in JavaScript to manage the Date objects. It is true that in vanilla JavaScript, there are various methods to manage date object but Moments JS makes it easier to use.

In this approach,

  • We will use the Moment JS subtract() library function to subtract the minutes from the given date.

  • To use the Moment JS library in the vanilla JavaScript, users need to add the CDN for the Moment JS in the < head > Section of the HTML code.


Syntax

Users can follow the below syntax to use the subtract() method of the Moment Js.

let newDate = moment().subtract( number_of_minuets, 'm').format( );

Parameters

  • number_of_minutes − It is the total minutes which we want to subtract from the given date object.

  • The ‘m’ flag given as a second parameter suggests that we need to subtract minutes given in the first parameters. However, users can easily change the ‘m’ flag and set different flags for years, months, days, hours, seconds, etc.

Example

In the below example, we have added the Moment JS CDN in the <head> section. We have created a new date object using the moment() and applied the subtract method with the second parameters ‘m’ to subtract the minutes from the current time.

<html>
<head>
   <title> Subtract the minutes from the date </title>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment-with-locales.min.js" integrity="sha512-vFABRuf5oGUaztndx4KoAEUVQnOvAIFs59y4tO0DILGWhQiFnFHiR+ZJfxLDyJlXgeut9Z07Svuvm+1Jv89w5g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</head>
<body>
   <h4> Subtracting the 100 minutes from the current time using Moment Js :</h4>
   <div id="momentOutput1"> </div>
   <script type="text/javascript">
      let momentOutput1 = document.getElementById("momentOutput1");

      // subtract 100 minutes from the current date
      newDate = moment().subtract(100, 'm').format();
      momentOutput1.innerHTML = newDate;
   </script>
</body>
</html>

Conclusion

This tutorial taught us to subtract the minutes from the given date. In the first approach, we have learned to subtract the minutes from the date using two methods getMinutes() and setMinutes(). In the third approach, the same work we have done by writing a single line of the code. So, Moment JS is the best library to solve the problem given in this tutorial.

raja
Updated on 20-Jul-2022 13:59:15

Advertisements