subtract 100 minutes from the current date Let momentOutput1 = document.getElementById("momentOutput1") Subtracting the 100 minutes from the current time using Moment Js : 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. In the below example, we have added the Moment JS CDN in the section. However, users can easily change the ‘m’ flag and set different flags for years, months, days, hours, seconds, etc. The ‘m’ flag given as a second parameter suggests that we need to subtract minutes given in the first parameters. Number_of_minutes − It is the total minutes which we want to subtract from the given date object. ![]() ![]() let newDate = moment().subtract( number_of_minuets, 'm').format( ) Parameters Users can follow the below syntax to use the subtract() method of the Moment Js. To use the Moment JS library in the vanilla JavaScript, users need to add the CDN for the Moment JS in the Section of the HTML code. We will use the Moment JS subtract() library function to subtract the minutes from the given date. It is true that in vanilla JavaScript, there are various methods to manage date object but Moments JS makes it easier to use. The Moment JS is the special library in JavaScript to manage the Date objects. 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. Let newDate = new Date(totalMilliSeconds - millisecondsToSubtract) Let totalMilliSeconds = oldDate.getTime() subtracting the 20 minutes from the T09:33:10 Subtracting the 20 minutes from the T05:30:40 : Subtract the Minutes from the date using the getTime() method. The below example demonstrate to subtract the minutes from the date in the form of milliseconds. It is the total minutes which users wants to subtract from the date. Minutes − Users need to replace minutes with the numeric value. Let newDate = new Date ( totalMilliSeconds – millisecondsToSubtract ) Parameters Let millisecondsToSubtract = minutes * 60 * 1000 Users can multiply the minutes by 60 seconds * 1000 milliseconds to convert the minutes tomilliseconds.īelow, users can see the syntax to use this approach. Subtract the Minutes from the Date in the Millisecond Formatįirst we will use the getTime() method to get the total milliseconds of the date.Īfter that, we will subtract the minutes from the total milliseconds in the form of milliseconds. In the second output, users can see that they will get date and time after subtracting the 40 minutes from the current time. subtracting the 40 minutes from the current time Let difference = newdate.getMinutes() - 132 substracting the 132 minutes from the J11:30:25 Let date2 = document.getElementById("date2") Let date1 = document.getElementById("date1") Subtract the 40 minutes from the current time. Subtract 132 minutes from the J11:30:25 : The getMinutes() and setMinutes() methods in JavaScript. The below example demonstrates the above approach. 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. Set_date − It is an optional parameter for the object of date class. Let difference = date.getMinutes() – minutes_to_substract // find difference between minutes tMinute( difference ) // set new minutes Parameters ![]() Syntax let date = new Date(set_date) // create new object of the date class. Users can follow the below syntax to use the getMinutes( ) and setMinutes( ) method. ![]() In the next step, we will set the new minutes to the date object using the setMinutes() method of the date class. In this approach, we will create a new date object and extract the minutes from that using the getMinutes() method.Īfter that, we will update the minutes by subtracting the minutes from the minutes we got from the date. Using the getMinutes() and setMinutes() 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. All JavaScript programmers are lucky enough that JavaScript contains the built-in date class with hundreds of different methods. While developing the applications, programmers need to play with the dates many times. In this tutorial, we will learn to subtract minutes from the date in JavaScript.
0 Comments
Leave a Reply. |