Using momentjs for the solution.
Angular-specific directive for moment.
Read more about Moment js here.
To start, install moment via bower:
bower install angular-moment moment --save
Make sure to include moment in your application:
<script src="components/moment/moment.js"></script>
<script src="components/angular-moment/angular-moment.js"></script>
( If using grunt, running grunt serve will automatically include these scripts after utilizing --save
during bower install
)
Add the angularMoment module to your app.
var myapp = angular.module('myapp', ['angularMoment']);
You can now utilize momentjs in your controllers.
It's recommended to use moment due to its simplicity when working with dates compared to pure javascript.
The issue at hand is determining if a specific time is before or after now within a specified timeframe (45 mins). Imagine a circle with a radius of 45 mins and now located at the center. Everything inside this circle signifies correctness while everything outside indicates incorrectness.
https://i.sstatic.net/JM5dn.png
In addition, it's important to identify whether something lies in the past, denoted by being on the left side of the circle.
For reference, let's consider the center as algebraic 0.
n represents the circle radius (45 mins).
If t is the argument (time provided), then to ascertain its position:
f(t) = n - t
If f(t) falls between 0 and t, it is on the right side.
If f(t) falls between t and 2t, it is on the left side (signifying the past).
Now, let's implement this logic using momentjs:
function checkTime(time){
var now = moment(new Date());
var later = now.add(45*60,'seconds');
var givenTime = moment(new Date(time));
var ft = later.millisecond() - time.millisecond();
if ( ( ft > 0 ) && ( ft < 2700000 ) )//45*60*1000
return false; // future
if ( ( ft > 2700000 ) && ( ft < 2*2700000 ) )
return false; // past
return true;
}