JQuery Moment JS Subtract And Addition Seconds to Datetime Example

Jquery

Nicesnippets

880

31-05-2021


JQuery Moment JS Subtract And Addition Seconds to Datetime Example

Hi Dev,

In this Tutorial I will explain you Jquery moment subtract seconds.i would like to share with you in this two example subtract seconds with moment.in this tutorial we will implement subtract seconds to current time. Let's get started with how to subtract seconds to time in jquery moment.

In this Article We will integrate two methods jquery moment js add() method for add seconds to date and jquery moment js subtract() method for subtract seconds from date.

Here, I will give you full example of both methods add() and subtract() you can check and copy code.

Solution 1: Subtract Seconds to Time


<!DOCTYPE html>

<html>

<head>

<title>JQuery Moment Subtract Seconds Example - NiceSnippets.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 Subtract Seconds Example - NiceSnippets.com</h1>

</body>

<script type="text/javascript">

var start_DateTime = '31-05-2021 11:00:00'

var new_DateTime = moment(start_DateTime, "DD-MM-YYYY hh:mm:ss")

.subtract(5, 'seconds')

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

console.log(new_DateTime);

var currentDateTime = moment().subtract(5, 'seconds')

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

console.log(currentDateTime);

</script>

</html>

Output

31/05/2021 10:59:55

31/05/2021 07:11:16

Solution 2: Add Seconds to Time

<!DOCTYPE html>

<html>

<head>

<title>JQuery Moment Add Seconds Example - NiceSnippets.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 Seconds Example - NiceSnippets.com</h1>

</body>

<script type="text/javascript">

var start_DateTime = '31-05-2021 11:30:00'

var new_DateTime = moment(start_DateTime, "DD-MM-YYYY hh:mm:ss")

.add(60, 'seconds')

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

console.log(new_DateTime);

var currentDateTime = moment().add(60, 'seconds')

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

console.log(currentDateTime);

</script>

</html>

Output

31/05/2021 11:31:00

31/05/2021 07:17:45

It Will Help You...


Recommended Posts