Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moment.js, how to get day of week number

Tags:

date

momentjs

People also ask

How do I find the day of the week number from a moment?

$(document). ready(function(){ var weeknumber = moment("12-25-1995", "MM-DD-YYYY"). week(); console. log(weeknumber); });

How do you find day from moment date?

To get day name from date with Moment. js and JavaScript, we can use the format method. const myDate = "2022-06-28T00:00:00"; const weekDayName = moment(myDate).

How do I get current week days in JavaScript?

JavaScript - Date getDay() Method Javascript date getDay() method returns the day of the week for the specified date according to local time. The value returned by getDay() is an integer corresponding to the day of the week: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.


Define "doesn't work".

const date = moment("2015-07-02"); // Thursday Feb 2015
const dow = date.day();
console.log(dow);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

This prints "4", as expected.


If you are specifically looking for the 1-7 approach...

This is the ISO weekday number. moment.js has also taken this into account. Use isoWeekday()

console.log(moment().isoWeekday()); // returns 1-7 where 1 is Monday and 7 is Sunday
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

Seeing as I wrote this answer on a Tuesday, today this gives me a 2.


I think this would work

moment().weekday(); //if today is thursday it will return 4

You can get this in 2 way using moment and also using Javascript

const date = moment("2015-07-02"); // Thursday Feb 2015
const usingMoment_1 = date.day();
const usingMoment_2 = date.isoWeekday();

console.log('usingMoment: date.day() ==> ',usingMoment_1);
console.log('usingMoment: date.isoWeekday() ==> ',usingMoment_2);


const usingJS= new Date("2015-07-02").getDay();
console.log('usingJavaSript: new Date("2015-07-02").getDay() ===> ',usingJS);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

From the docs page, notice they have these helpful headers

http://momentjs.com/docs/#/get-set/weekday/
(I didn't see them at first)

With header sections for:

  • Date of Month
  • Day of Week
  • etc

.

  var now = moment();
  var day  = now.day();
  var date = now.date(); // Number