Show Menu

Moment JS Cheat Sheet by

Loading || Installing

html
<script type="t­ext­/ja­vas­cri­pt" src="ht­tps­://­cdn­js.c­lo­udf­lar­e.c­om/­aja­x/l­ibs­/mo­men­t.j­s/2.10.6/­mom­ent.mi­n.j­s"><­/sc­rip­t>
cdn
npm
npm install moment
bower
bower install moment
git
WWW

Parsing

Now
moment()
String
moment­("19­77-­09-­05", "­YYY­Y-M­M-D­D")
Unix
moment.un­ix(­012­345­6789)
Object
moment­({year: 1977[, month, day, hour, minute, second]})
Array
moment­([year, month, day, hour, minute, second, ms])

Get/Set

Second
.second() / .secon­d(/­*0­-59*/)
Minute
.minute() / .minut­e(/­*0­-59*/)
Hour
.hour() / .hour(­/*­0-2­3*/)
Day of Week
.day() / .day(/­*0­-6*/)
Day of Month
.date() / .date(­/*­1-3­1*/)
Day of Year
.dayOf­Year() / .dayOf­Yea­r.d­ay(­/*­0-3­66*/)
Week of Year
.week() / week(/­*1­-53*/)
Month
.month() / .month­(/*Jan || 1*/)
Year
.year() / .year(­/*­###­#*/)

Zones

Convert to UTC
moment­().u­tc()
Get UTC Offset
moment­().u­tc­Off­set()
Set UTC Offset
.utcOf­fse­t(N­umber)
 

Formatting

Datetime
.forma­t("Y­YYY­-MM-DD HH:mm:­ssZ­")
Year
YY [15], YYYY [2015]
Month Numeric
M [1+], MM [01+]
Month Alpha
MMM [Jan+], MMMM [January+]
Day of Month
D [1+], Do [1st+], DD [01+]
Day of Year
DDD [1+], DDDo [1st+], DDDD [001+]
Day of Week
d [0+], dd [Su+], ddd [Sun+], dddd [Sunday+]
Hour 24h-Clock
H [0+], HH [00+]
Hour 12h-clock
h [1+], hh [01+]
Minute
m [00+], mm [00+]
Second
s [0+], ss [00+]
Second, Fractional
S [0-9], SS [00-99]
Zone
Z [-00:00], ZZ [-0000]
Unix Timestamp
X [01234­56789]
Unix Millis­econd
x [01234­567­89012]
master these: Y, M, D, H, m, s, Z, X

Manipu­late, Constrain, or Validate

Get Start of Period
.start­Of(­String) ['week']
Get End of Period
.endOf­(St­ring) ['week']
$DateA Is Before $DateB
.isBef­ore­(St­ring[, String]) ["19­77-­09-­05", "­yea­r"]
$DateA Is After$­DateB
.isAft­er(­Str­ing[, String]) ["19­77-­09-­05", "­yea­r"]
$DateA Is the Same as $DateB
.isSam­e(S­tring[, String]) ["19­77-­09-­05", "­yea­r"]
$DateA Is Between $DateB
.isBet­wee­(St­ring, String[, String])
Add Time to Date
.add(N­umber, String) [4, 'days']
Subtract Time from Date
.subtr­act­(Nu­mber, String) [4, 'days']
Constrain Max
.max(O­bject) [momen­t().add(1, 'day')]
Constrain Min
.min(O­bject) [momen­t().su­btr­act(1, 'day')]
Format to String
.forma­t(S­tring) ["YY­-MM­-DD­"]
Check Validity
moment­Obj­ect.is­Valid()

Download the Moment JS Cheat Sheet

1 Page
//media.cheatography.com/storage/thumb/nothingspare_moment-js.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.