Skip to content

A Moment.js tutorial

New Courses Coming Soon

Join the waiting lists

Moment.js is a great help in managing dates in JavaScript

Moment.js is an awesome JavaScript library that helps you manage dates, in the browser and in Node.js as well.

This article aims to explain the basics and the most common usages of this library.


You can include it directly in your page using a script tag, from

<script src="" />

or using npm:

npm install moment

If you install using npm you need to import the package (using ES Modules):

import moment from 'moment'

or require it (using CommonJS):

const moment = require('moment')

Get the current date and time

const date = moment()

Parse a date

A moment object can be initialized with a date by passing it a string:

const date = moment(string)

it accepts any string, parsed according to (in order):

ISO 8601 is definitely the most convenient. Here’s a format reference:

YYYY4-digits Year2018
YY2-digits Year18
M2-digits Month number, omits leading 07
MM2-digits Month number07
MMM3-letters Month nameJul
MMMMFull Month nameJuly
ddddFull day nameSunday
gggg4-digits Week year2018
gg2-digits Week year18
wWeek of the year without leading zero18
wwWeek of the year with leading zero18
eDay of the week, starts at 04
D2-digits day number, omits leading 09
DD2-digits day number09
DoDay number with ordinal9th
TIndicates the start of the time part
HH2-digits hours (24 hour time) from 0 to 2322
H2-digits hours (24 hour time) from 0 to 23 without leading 022
kk2-digits hours (24 hour time) from 1 to 2423
k2-digits hours (24 hour time) from 1 to 24 without leading 023
a/Aam or pmpm
hh2-digits hours (12 hour time)11
mm2-digits minutes22
ss2-digits seconds40
s2-digits seconds without leading zero40
S1-digits milliseconds1
SS2-digits milliseconds12
SSS3-digits milliseconds123
ZThe timezone+02:00
xUNIX timestamp in milliseconds1410432140575

Set a date

Format a date

When you want to output the content of a plain JavaScript Date object, you have little options to determine the formatting. All you can do is to use the built-in methods, and compose the date as you want using them.

Moment offers a handy way to format the date according to your needs, using the format() method:


The string format accepts the same formats I described in the “Parse a date” section above.


moment().format("YYYY Do MM")

Moment provides some constants you can use instead of writing your own format:


Validating a date

Any date can be checked for validity using the isValid() method:

moment('2018-13-23').isValid() //false
moment('2018-11-23').isValid() //true

Time ago, time until date

Use fromNow(). Strings are localized:

moment('2016-11-23').fromNow() //2 years ago
moment('2018-05-23').fromNow() //a month ago
moment('2018-11-23').fromNow() //in 5 months

if you pass true to fromNow(), it just shows the difference, without reference to future/past.

moment('2016-11-23').fromNow(true) //2 years
moment('2018-05-23').fromNow(true) //a month
moment('2018-11-23').fromNow(true) //5 months

Manipulate a date

You can add or subtract any amount of time to a date:

moment('2016-11-23').add(1, 'years')
moment('2016-11-23').subtract(1, 'years')

You can use those values:

→ Get my JavaScript Beginner's Handbook
→ Read my JavaScript Tutorials on The Valley of Code
→ Read my TypeScript Tutorial on The Valley of Code

Here is how can I help you: