moment.js, how to get day of week number

2019-01-26 02:56发布

I have a moment date object, and want to get the selected day number (0-6) or (1-7).

I tried this, but it doesn't work

var aaa= moment(date).day();

help me with this please

标签: date momentjs
4条回答
2楼-- · 2019-01-26 03:08

I think this would work

moment().weekday(); //if today is thursday it will return 4
查看更多
狗以群分
3楼-- · 2019-01-26 03:22

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()

moment().isoWeekday(); // returns 1-7 where 1 is Monday and 7 is Sunday

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

查看更多
爷的心禁止访问
4楼-- · 2019-01-26 03:22

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
查看更多
趁早两清
5楼-- · 2019-01-26 03:28

Define "doesn't work".

var date = moment("2015-07-02");
var dow = date.day();
console.log(dow);

This prints "4", as expected.

查看更多
登录 后发表回答