I'll-Be-Back
I'll-Be-Back

Reputation: 10828

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.

Upvotes: 3

Views: 109

Answers (2)

Alex
Alex

Reputation: 21766

You could use moment.weekdays(), see example below, but please check out James Donnelly answer as well.

var weekdayInt = 3
var weekdays = moment.weekdays()
document.write(weekdays[weekdayInt])
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/momentjs/2.16.0/moment.min.js"></script>

Upvotes: 2

James Donnelly
James Donnelly

Reputation: 128791

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.

Upvotes: 6

Related Questions