JQuery Moment Subtract Minutes to Datetime Example

By Hardik Savani December 31, 2020 Category : jQuery

This post is focused on jquery moment subtract minutes. Here you will learn moment js subtract minutes to time. We will look at example of moment subtract minutes to current time. you can understand a concept of moment js subtract minutes to current time. Let's get started with how to subtract minutes to time in jquery moment.

Here, i will give you simple example of jquery moment js add() method for add minutes to date and jquery moment js subtract() method for subtract minutes from date.

let's see both example with output:

Example 1: Subtract Minutes to Time

<!DOCTYPE html>

<html>

<head>

<title>JQuery Moment Add Minutes Example - ItSolutionStuff.com</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" crossorigin="anonymous"></script>

</head>

<body>

<h1>JQuery Moment Add Minutes Example - ItSolutionStuff.com</h1>

</body>

<script type="text/javascript">

var startDateTime = '26-12-2020 09:30:20'

var newDateTime = moment(startDateTime, "DD-MM-YYYY hh:mm:ss")

.subtract(10, 'minutes')

.format('DD/MM/YYYY hh:mm:ss');

console.log(newDateTime);

var currentDateTime = moment().subtract(10, 'minutes')

.format('DD/MM/YYYY hh:mm:ss');

console.log(currentDateTime);

</script>

</html>

Output:

26/12/2020 09:20:20

31/12/2020 09:10:09

Example 2: Add Minutes to Time

<!DOCTYPE html>

<html>

<head>

<title>JQuery Moment Add Minutes Example - ItSolutionStuff.com</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" crossorigin="anonymous"></script>

</head>

<body>

<h1>JQuery Moment Add Minutes Example - ItSolutionStuff.com</h1>

</body>

<script type="text/javascript">

var startDateTime = '26-12-2020 09:30:20'

var newDateTime = moment(startDateTime, "DD-MM-YYYY hh:mm:ss")

.add(10, 'minutes')

.format('DD/MM/YYYY hh:mm:ss');

console.log(newDateTime);

var currentDateTime = moment().add(10, 'minutes')

.format('DD/MM/YYYY hh:mm:ss');

console.log(currentDateTime);

</script>

</html>

Output:

26/12/2020 09:40:20

31/12/2020 09:26:44

I hope it can help you...

Tags :
Shares