如何使用时刻比较星期几

时间:2016-04-13 16:52:59

标签: javascript momentjs

我有这样的结构:

<ul id="list">
   <li data-day="monday">BlaBla</li>
   <li data-day="friday">BlaBla</li>
   <li data-day="tuesday">BlaBla</li>
</ul>  

我想阅读该数据属性,然后检查代表星期几的NUMBER,例如:

   $('#list').each(function(){
       var day = $($this).data('day'); //day = monday
       //MAKE SOMETHING BETTER THAN A BUNCH OF 'IF'S CONDICIONAL
       //TO COMPARE THE DAY AND RELATE IT TO THE NUMBER OF THE WEEK.
       //Now using `momentjs` I need to relate monday with the number day of the week. Knowing that monday is equivalent of 1.   
       if(day = "monday"){ var numDay = 1}
       else if(day = "sunday"){ var numDay = 2}



});

1 个答案:

答案 0 :(得分:1)

这应该这样做:

console.log(moment().day("Monday").day());

docs