Navigate back to the homepage

how to get am and pm in moment js?

Infinitbility
MomentJS
June 14th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To get am and pm in moment js, just use the moment().format() method by passing desire format string parameters with a at the end.

Just import moment in your file and invoke moment().format('hh:mm:ss a'); it will show the current time with am and pm.

1moment().format('hh:mm:ss a')

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

Let’s start the today’s tutorial How do you get am and pm 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. example of date time format with am and pm DD-MM-YYYY h:mm:ss a
  3. example of time format with am and pm h:mm:ss a

let’s write the code.

1import moment from "moment";
2
3function App() {
4 return (
5 <div>
6 {/* example of date time format with am and pm `DD-MM-YYYY h:mm:ss a` */}
7 {console.log(moment().format("DD-MM-YYYY h:mm:ss a"))}
8 {console.log("----")}
9 {/* example of time format with am and pm `h:mm:ss a` */}
10 {console.log(moment().format("h:mm:ss a"))}
11 </div>
12 );
13}
14
15export default App;

In the above program, we call moment().format() with format string to show date time with am and pm.

let’s check the output.

MomentJs, Date time with am and pm example
MomentJs, Date time with am and pm 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 subtract two dates in moment js?

Example of subtract two dates in moment js and getting the difference in seconds, minutes, hours, days months, or years with step by step guide.

June 14th, 2022 · 1 min read

how to convert string to date in moment js?

Example of convert string to date in moment js with step by step guide.

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