Navigate back to the homepage

how to convert string to date in moment js?

Infinitbility
MomentJS
June 13th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To convert string to date in moment js, just use the moment().format() method by passing desire format string parameters.

Just import moment in your file and invoke moment().format('DD-MM-YYYY'); it will show the current date time plus 30 years.

1moment().format('DD-MM-YYYY')

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

Let’s start the today’s tutorial How do you convert string to date in moment js?

Table of content

  1. Installation
  2. Moment Format rules
  3. 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

Moment Format rules

Moment follow below rules to format date time as per user requirement like show am, pm in time string, show two digits year, and show date with st, nd, and rd.

InputExampleDescription
YYYY20144 or 2 digit year. Note: Only 4 digit can be parsed on 'strict' mode
YY142 digit year
Y-25Year with any number of digits and sign
Q1..4Quarter of year. Sets month to first month in quarter.
M MM1..12Month number
MMM MMMMJan..DecemberMonth name in locale set by moment.locale()
D DD1..31Day of month
Do1st..31stDay of month with ordinal
DDD DDDD1..365Day of year
H HH0..23Hours (24 hour time)
h hh1..12Hours (12 hour time used with a A.)
k kk1..24Hours (24 hour time from 1 to 24)
a Aam pmPost or ante meridiem (Note the one character a p are also considered valid)
m mm0..59Minutes
s ss0..59Seconds

After reading the above rules, you can easily understand how to format time as per your requirement. let’s see how we can use the format() method with the above rules.

Example in reactjs

In the following example, we are going to do

  1. import the moment package
  2. example of show date using date string
  3. example of show date time using date string

let’s write the code.

1import moment from "moment";
2
3function App() {
4 return (
5 <div>
6 {/* example of show date using date string */}
7 {console.log(moment('2012-02-27T10:00:00').format("DD-MM-YYYY"))}
8 {console.log("----")}
9 {/* example of show date time using date string */}
10 {console.log(moment('2012-02-27T10:00:00').format("DD-MM-YYYY h:mm:ss a"))}
11 </div>
12 );
13}
14
15export default App;

In the above program, we call moment().format() with date string and format string.

let’s check the output.

MomentJs, Convert string to date example
MomentJs, Convert string to date 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 format time in moment js?

Example of format time in moment js with step by step guide.

June 13th, 2022 · 1 min read

how to add days in moment js?

Example of add days in date object using moment js with step by step guide.

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