我有一个时刻日期对象,想要获得所选的日期编号(0-6)或(1-7).
我试过这个,但它不起作用
var aaa= moment(date).day();
Run Code Online (Sandbox Code Playgroud)
请帮帮我
小智 123
定义"不起作用".
const date = moment("2015-07-02");
const dow = date.day();
console.log(dow);Run Code Online (Sandbox Code Playgroud)
按预期打印"4".
mjo*_*ngr 76
如果你是专门寻找1-7方法......
这是ISO工作日编号.moment.js也考虑到了这一点.使用isoWeekday()
console.log(moment().isoWeekday()); // returns 1-7 where 1 is Monday and 7 is SundayRun Code Online (Sandbox Code Playgroud)
看到我在星期二写了这个答案,今天这给了我一个2.
Tar*_*pta 21
我认为这会奏效
moment().weekday(); //if today is thursday it will return 4
Run Code Online (Sandbox Code Playgroud)
您可以使用 moment 和 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);Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>Run Code Online (Sandbox Code Playgroud)