Eljas Eljas - 2 months ago 9
Javascript Question

Check if time is greater than specific time in a day

Probably asked before but can't find answer for this. How do I check if time is greater than 17:30 every day?

My case is that I would need to check if current time is greater than 17:30 on Monday to Friday and if today is Saturday I have to check if time is greater than 15:30.

I do prefer use of Moment.js.

Answer

You can just check for the time using Date(). Seriously, you don't need a big plugin for this:

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!");

Let me know if there's a case, this fails!