Navigate back to the homepage

how to add seconds in moment js?

Infinitbility
MomentJS
June 11th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To add seconds in the date using moment js, just use the moment().add() method by passing two parameters, first is the number of seconds you want to add, and the second parameter is 'seconds' it’s means want to add seconds.

Just import moment in your file and invoke moment().add(30, 'seconds'); it will show the current date time plus 30 seconds.

1moment().add(20, 'seconds');

Today, I’m going to show How do I add seconds in moment js, here I will use the momentjs common method add() to add seconds in the date object.

Let’s start the today’s tutorial How do you add seconds in the date using moment js?

Table of content

  1. Installation
  2. Example in reactjs

Installation

Use the below installation command as per your package manager, moment support npm, Yarn, NuGet, spm, and meteor.

1npm install moment --save # npm
2yarn add moment # Yarn
3Install-Package Moment.js # NuGet
4spm install moment --save # spm
5meteor add momentjs:moment # meteor

Example in reactjs

In the following example, we are going to do

  1. import the moment package
  2. create a function and console the current time.
  3. add seconds in the current time and re-console it.

let’s write the code.

1import moment from "moment";
2
3// create a function and console moment
4function consoleTheMoment() {
5 let currentDateTime = moment();
6 console.log("currentDateTime", currentDateTime)
7 // add seconds in current time and re console it.
8 currentDateTime = moment(currentDateTime).add(30, 'seconds')
9 console.log("currentDateTime plus 30s", currentDateTime)
10}
11
12function App() {
13 consoleTheMoment()
14 return (
15 <div>
16 {/* moment in react render */}
17 {moment().format("hh:mm:ss")}
18 </div>
19 );
20}
21
22export default App;

In the above program, we call moment().add() in simple custom function and and add 30 seconds in it.

let’s check the output.

MomentJs, add seconds example
MomentJs, add seconds example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to compare two dates in moment js?

Example of compare two dates in moment js with step by step guide.

June 11th, 2022 · 2 min read

How to get difference between two dates in weeks in javascript?

Example of get weeks between two dates in javascript with step by step guide.

June 10th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]