欧洲编号中的星期几

Day of the week in the European numbering

本文关键字:编号      更新时间:2023-09-26

此函数工作正常。

function getLocalDay(date) {
   return (date.getDay() == 0) ? 7 : date.getDay();
}
alert( getLocalDay( new Date(2016, 0, 10) ) ); // 7

如果我编辑条件date.getDay() ? 7 : date.getDay()-函数工作不正常。如果是星期日,函数应返回7。

当然!如果你想缩短你的方法,你必须写:

return !date.getDay() ? 7 : date.getDay();

你忘了!

你也可以像@Akxe在评论中写的那样写date.getDay() || 7

这将正常工作。date.getDay()将在sundays返回0,0是一个伪值,因此将调用条件的第二个分支。

function getLocalDay(date) {
   return date.getDay() ? date.getDay() : 7;
}
alert( getLocalDay( new Date(2016, 0, 10) ) );

https://jsfiddle.net/rwgqjegb/

参考:Truthy和Falsey