检查时间是否大于一天中的特定时间

Elj*_*jas 0 javascript momentjs

可能之前问过但是找不到答案.如何检查时间是否每天超过17:30?

我的情况是我需要检查周一到周五当前时间是否大于17:30,如果今天是星期六,我必须检查时间是否大于15:30.

我更喜欢使用Moment.js.

Ste*_*tis 6

这是一个例子 moment.js

function check() {
  var now = moment();
  var hourToCheck = (now.day() !== 0)?17:15;
  var dateToCheck = now.hour(hourToCheck).minute(30);
  
  return moment().isAfter(dateToCheck);
}

console.log(check())
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
Run Code Online (Sandbox Code Playgroud)


Pra*_*man 5

您可以使用 来检查时间Date()。说真的,你不需要一个大插件:

var curTime = new Date();
var day = curTime.getDay();
curTime = parseInt(curTime.getHours() + "" + ("0" + curTime.getMinutes()).substr(-2) + "" + ("0" + curTime.getSeconds()).substr(-2));

if ((curTime > 173000 && day > 0 && day < 6) || (curTime > 153000 && day <= 0 && day >= 6))
  console.log("It's a good time!");
else
  console.log("It's not a good time!");
Run Code Online (Sandbox Code Playgroud)

如果有情况请告诉我,这失败了!