MomentJS - String


This will take string as date for parsing with moment.



Observe the following examples and their outputs when different date strings are given to moment.

Example 1

var day = moment("2017-04-15");

To display the date, we have used day._d to get the date details from the moment.



Example 2

var day = moment("2017-W10-5");


var day = moment("2017W105");

Observe that in the string "2017-W10-5" given to the moment, W represents the week. You can observe the following output, where W10 falls for March month.


String Week

Example 3

var day = moment("2017-080");


var day = moment("2017080");

Here, the string 2017-080 is the 80th day which falls on March 21st as shown below.


String March

Example 4

var day = moment("2017-05-08T09");


var day = moment("20170508T09");

Here, the string 2017-05-08T09 is given to moment where the number after T represents the hour to be shown.


String Hour

Example 5

var day = moment("2017-06-08 06:30:26");


var day = moment("20170608T063026");

We can also pass the hour, minutes, or seconds to the moment and the output as follows −


String Moment

Moment with String Format

The table given below shows the format details for year, month, and day.

Format Example Details
YYYY 2018 Displays 4 digit year
YY 18 Displays 2 digit year
Q 1-4 Displays the Quarter
M or MM 1-12 Month number
MMM or MMMM Jan-Dec or January - December Name of the month
D or DD 1-31 Day of month
Do 1st-31st Day of month with ordinal
DDD or DDDD 1-365 Day of year
X 1598773566.565 Unix Timestamp
x 1598773566565 Unix Timestamp in milliseconds

The table given below shows the format details for week, weekyear and week days −

Input Example Details
gggg 2018 Locale 4 digit week year
gg 18 Locale 2 digit week year
w or ww 1-53 Week of the year
e 0-6 Day of week
ddd or dddd Mon-Sun or Monday-Sunday Name of the day in the week
GGGG 2018 ISO 4 digit year
GG 18 ISO 2 digit year
W or WW 1-53 ISO week of the year
E 1-7 ISO day of the week

The table given below shows the format details for hour, minute, seconds , milliseconds −

Format Example Details
H or HH 0-23 24 hrs time
h or hh 1-12 12 hrs time
k or kk 1-24 24 hrs time starting from 1
a A am pm Post or ante meridian
m or mm 0-59 minutes
s or ss 0-59 seconds
S or SS or SSS 0-999 Fractional seconds
Z or ZZ +12:00 Offset from UTC as +-HH:mm, +-HHmm, or Z

You can check if date is valid as per the string formats using the command as shown −

var day = moment('2018.05.25', 'YYYY-MM-DD').isValid();

As you can observe in the output shown below, this will return true since the date is in proper format: YYYY, MM, and DD.

String Six

If the same date is changed as given below, the output will be false, as shown below −

var day = moment('05.25', 'YYYY-MM-DD').isValid();
String False

You can also format the date as per your requirement as shown in the following examples −

Example 1

var day = moment('2018/05/25').format("YYYY-MM-DD");


String Format Date

Example 2

var day = moment('20170608T063026').format("YYYY-MM-DD HH:mm:ss");



Example 3

var day = moment("634", "Hmm").format("HH:mm");


String HHMM


It is possible to parse multiple formats, where the formats are passed in array form as shown below −

var day = moment("12-25-1995",["MM-DD-YYYY", "YYYY-MM-DD"]).isValid();


String Multiple Format

Since the given date matches with one of the formats, the output given is true.