Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 681
Eljas Avatar asked Dec 01 '22 16:12

Eljas


2 Answers

Here an example with 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())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
like image 56
Steeve Pitis Avatar answered Dec 03 '22 05:12

Steeve Pitis


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!

like image 27
Praveen Kumar Purushothaman Avatar answered Dec 03 '22 05:12

Praveen Kumar Purushothaman