How to Subtract Seconds to Datetime in Jquery Moment Js?

May 17, 2024 | jQuery


Hello Dev,

This tutorial will illustrate how to subtract seconds using jQuery and Moment.js. We'll focus on Moment.js' capability to subtract seconds from time. Throughout this article, we'll explore the process of subtracting seconds from the current time using Moment.js. Let's begin by understanding how to subtract seconds from time in jQuery Moment.

Here, I'll provide a straightforward example of utilizing Moment.js' add() method to add seconds to a date and the subtract() method to subtract seconds from a date.

Read Also: Jquery Moment js Convert String to Date Example
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>How to Subtract Seconds to Datetime in Jquery Moment Js? -- ItErrorSolution.com</title>
    <!-- jQuery -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <!-- Moment.js -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
</head>
<body>
<p id="result"></p>

<script>
$(document).ready(function() {
    // Define the number of seconds to subtract
    var secondsToSubtract = 60; // Example: subtract 60 seconds
    
    // Get the current time
    var currentTime = moment();
    
    // Subtract seconds from the current time
    var newTime = currentTime.subtract(secondsToSubtract, 'seconds');
    
    // Display the new time
    $('#result').text("New Time: " + newTime.format('DD/MM/YYYY HH:mm:ss'));
});
</script>
</body>
</html>
Output:
New Time: 17/05/2024 08:24:33
Explanation:

jQuery Ready Function: The $(document).ready() function ensures that the script runs after the entire page has loaded.
Seconds to Subtract: Define the number of seconds you want to subtract from the current time.
Current Time: moment() gets the current time.
Subtracting Seconds: .subtract(secondsToSubtract, 'seconds') subtracts the specified number of seconds from the current time.
Formatting the Result: .format('HH:mm:ss') formats the resulting time in hours, minutes, and seconds.
Displaying the Result: $('#result').text(...) sets the text content of the #result paragraph to the new time.

This example demonstrates how to use jQuery and Moment.js to subtract seconds from the current time, providing a practical way to manipulate and display dynamic times based on user-defined intervals.

Thank you for your encouragement! If you have any questions or need further assistance, feel free to ask. I'm here to help!



Tags :
#jQuery
ItErrorSolution.com

ItErrorSolution.com

"Hey there! I'm a full-stack developer and proud owner of ItErrorSolution.com, based right here in India. I love nothing more than sharing handy tips and tricks with my fellow developers through easy-to-follow tutorials. When it comes to coding, I'm all about PHP, Laravel, Angular, Vue, Node, JavaScript, jQuery, CodeIgniter, and Bootstrap – been hooked on them forever! I'm all about putting in the work and staying committed. Ready to join me on this journey to coding?"