javascript - Angular function to check if time is within 45mins of given time and if now is after time -
i'm new 'angularjs' , having hard time creating function check if give 'time' less 45 minutes now , if time in past now after time
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); return (minutes > 40 || (minutes < 0 && minutes > -1395)); } this have far, checks time not greater 45 mins now
any guidance appreciated!
solution momentjs.
first install moment through bower:
bower install angular-moment moment --save include moment in app:
<script src="components/moment/moment.js"></script> <script src="components/angular-moment/angular-moment.js"></script> ( if using grunt, run grunt serve , automatically add these scripts, since using --save during bower install )
add module angularmoment app.
var myapp = angular.module('myapp', ['angularmoment']); now can use momentjs in controllers. suggest moment, because it's easier operate on dates pure javascript.
your problem find 1 point in time before or after in specified time ( 45 mins ). seems me imagine have circle radius. radius 45 mins , in middle of circle. inside circle fine , outside wrong. 
you want know if in past, on left side of circle.
so lets assume middle algebraic 0.
n circle radius. ( 45 mins )
if t argument ( given time ), check if inside:
f(t) = n - t if f(t) between 0 , t belongs on right side. if f(t) between t , 2t belonds , in on left side ( past ). with preparation, time implement in momentjs:
function checktime(time){ var = moment(new date()); var later = now.add(45*60,'seconds'); var time = 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; }
Comments
Post a Comment