Check for a time difference of less than 45 minutes and the launch function - AngularJS

This is easy to do in PHP with this code:

if (strtotime($given_time) >= time()+300) echo "You are online"; 

But can't find anything on SO to do exactly that in javascript. I want to check if the difference between the set time and the current time is less than 45 minutes.

for instance

 $scope.given_time = "14:10:00" $scope.current_time = new Date(); 

I'm only interested in part of the time. I need to extract the temporary part from new Date(); and then compare.

Then it should be true

How can I achieve this with Javascript:

 if ($scope.given_time - $scope.current_time < 45 minutes) { // do something } 
+5
source share
4 answers

Try the following:

 function checkTime(time) { var date = new Date(); var date1 = new Date((date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear() + " " + time); var minutes = (date1.getTime() - date.getTime()) / (60 * 1000); if (minutes > 45 || (minutes < 0 && minutes > -1395)) { // greater than 45 is todays time is above 45 minutes // less than 0 means the next available time will be tomorrow and the greater than -1395 means it will be more than 45 minutes from now into tomorrow document.write(time + ': true<br />'); } else { document.write(time + ': false<br />'); } } checkTime("14:10:00"); checkTime("16:30:00"); checkTime("17:10:00"); 
0
source

Javascript uses unix timestamps in milliseconds, so it looks like strtotime output (which uses seconds).

 var date = new Date(); 

Then you will need to perform the calculation in milliseconds. (Minutes * 60 * 1000)

You can also use date.parse() to parse a string in milliseconds, just like strtotime() in PHP does up to seconds.

In full:

 var date = new Date(); var last = new Date('Previous Date'); // or a previous millisecond timestamp if ( ( date - last ) > ( 45 * 60 * 1000 ) ) { // do something } 

You can use a static date to compare just time, this is exactly what strtotime does if you exclude the date:

 var last = new Date('1/1/70 14:10:00'); var date = new Date('1/1/70 14:30:00'); 

However, this approach will not succeed if you are trying to compare time crossing the borders of the day.

+2
source

There is a JavaScript method called getMinutes (); you can use to get only minutes and compare.

Your code should look something like this:

  var received_time = "14:10:00".split(':'); var minute = ''; if(received_time.length === 3) { minute = parseInt(received_time[1], 10); } $scope.given_time = minute; var the_time = new Date(); $scope.current_time = the_time.getMinutes(); 

And now you can do your job:

 if ($scope.given_time - $scope.current_time < 45 minutes) { // do something } 
0
source

Using a library like moment.js, you can simply distinguish between two times.

 var $log = $("#log"); /* Difference between just times */ $log.append("Difference between times\n"); var givenTime = moment("14:10:00", "HH:mm:ss"); var minutesPassed = moment("14:30:00", "HH:mm:ss").diff(givenTime, "minutes"); $log.append("Minutes passed: " + minutesPassed + "\n"); if (minutesPassed < 45) { $log.append(minutesPassed + " minutes have elapsed. Event Triggered." + "\n"); } /* Better: Difference between times that have dates attached to them and cross a day boundary. */ $log.append("\n\nDifference between dates with times\n"); givenTime = moment("2015-12-03 23:33:00"); minutesPassed = moment("2015-12-04 00:14:00").diff(givenTime, "minutes"); $log.append("Minutes passed: " + minutesPassed + "\n"); if (minutesPassed < 45) { $log.append(minutesPassed + " minutes have elapsed. Event Triggered." + "\n"); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://momentjs.com/downloads/moment.js"></script> <p>Results:</p> <hr> <pre id="log"></pre> <hr> 

Caution: If a given time has passed yesterday, for example, 11:30 pm, and the current time is 12:10, you will get the wrong result. You want to use a date over time if this type of problem is a problem for your use case.

Documentation moment.js

http://momentjs.com/docs/

Angular directive for documentation of the moment

https://github.com/urish/angular-moment/blob/master/README.md

0
source

Source: https://habr.com/ru/post/1237410/


All Articles