Navigate back to the homepage

how to get current time in moment js?

Infinitbility
MomentJS
June 10th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To get current time in moment js, just call moment().format() with "hh:mm:ss" time parameter format, it will return current time.

Just import moment in your file and invoke moment().format("hh:mm:ss").

1moment().format("hh:mm:ss")

Today, I’m going to show How do I get the current time in moment js, here I will use the momentjs common method moment().format() to the current time.

Let’s start the today’s tutorial How do you get the current time in 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. use moment in react render to print current time.

let’s write the code.

1import moment from "moment";
2
3// create a function and console moment
4function consoleTheMoment() {
5 console.log(moment().format("hh:mm:ss"));
6}
7
8function App() {
9 consoleTheMoment()
10 return (
11 <div>
12 {/* moment in react render */}
13 {moment().format("hh:mm:ss")}
14 </div>
15 );
16}
17
18export default App;

In the above program, we call moment().format() in simple function and in react render method to get the current time using moment.

let’s check the output.

javascript, get current time in moment js example
javascript, get current time in moment js 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 get months between two dates in javascript?

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

June 9th, 2022 · 1 min read

how to get year difference between two dates in javascript?

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

June 9th, 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]