I'll-Be-Back I'll-Be-Back - 19 days ago 5
Javascript Question

Return day of the week from int 1-7

Using

moment.js
how to return day of the week?

I have tried the following:

var i = 3;
moment(i).format('dddd');


Expected to return Wednesday which it did not work.

Answer

You need to let Moment know what the input format is. In your case, the input format is the day of the week, which Moment defines as 'e' or 'E' depending on whether you're wanting it 0-indexed or not:

moment(i, 'e').format('dddd') // if 0-6
moment(i, 'E').format('dddd') // if 1-7
Input Example Description
e     0..6    Locale day of week
E     1..7    ISO day of week

– Moment's documentation on Week year, week, and weekday tokens.

Comments