JQuery Moment Add Seconds to Datetime Example

By Hardik Savani January 1, 2021 Category : jQuery

In this tutorial we will go over the demonstration of jquery moment add seconds. i would like to share with you moment js add seconds to time. if you want to see example of moment add seconds to current time then you are a right place. i would like to share with you moment js add seconds to current time.

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

let's see both example with output:

Example 1: Add Seconds to Time

<!DOCTYPE html>

<html>

<head>

<title>JQuery Moment Add Seconds 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 Seconds 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, 'seconds')

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

console.log(newDateTime);

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

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

console.log(currentDateTime);

</script>

</html>

Output:

26/12/2020 09:30:30

31/12/2020 09:27:24

Example 2: Subtract Seconds to Time

<!DOCTYPE html>

<html>

<head>

<title>JQuery Moment Add Seconds 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 Seconds 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(2, 'seconds')

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

console.log(newDateTime);

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

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

console.log(currentDateTime);

</script>

</html>

Output:

26/12/2020 09:30:10

31/12/2020 09:28:47

I hope it can help you...

Tags :
Shares