uber
uber

Reputation: 5083

How to transform a date into day of the week counting from 0 on Sunday with Moment.js?

I am currently using const weekday = moment().isoWeekday() to get which day of the week it is. It starts with Sunday as 0 and ends with Saturday as 6.

Now, I am getting a date in the following format, as a string: 06-07-2020

How could I use moment to find out which day of the week (in numbers as above, starting with Sunday as 0) this date is?

Upvotes: 1

Views: 184

Answers (2)

MattJHoughton
MattJHoughton

Reputation: 1118

If the day function is returning NaN then it's not reading your date properly. Tell Moment what date format it should expect, before asking it what day it is:

moment("06-07-2020", "MM-DD-YYYY").day();

OR

moment("06-07-2020", "DD-MM-YYYY").day();

Upvotes: 2

Ashutosh Kumar
Ashutosh Kumar

Reputation: 907

You may easily get the day of the week using the .day() function with momentjs.

const date = moment("06-07-2020","DD-MM-YYYY") //Today
let day_of_week = date.day()
console.log(day_of_week)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>

Upvotes: 2

Related Questions