Elwyn Elwyn - 2 months ago 16
AngularJS Question

Is it possible to prevent an AngularJS app to start during business hours?

Is it possible to prevent an AngularJS app to start during business hours ?

Let's say that I do not want the app to be working between 8h and 18h. During this time, I want to display a page saying that the user must come back at

the time left
.

Answer

Yes it's possible.

JavaScript allows to get date, and time using Date object. For example

var date = new Date(); // Will set date variable as current date object
console.log(new Date().getTime()) // Will print time (milliseconds since midnight of January 1, 1970)

You can further process the date using Moment JS library - http://momentjs.com/

Using Moment JS you can extract hour, and minute and set simple boolean variable in scope. Let's name it businessHours.

Then you can create two div elements with ng-show/ng-hide commands:

<div ng-hide='businessHours'>
   <!-- application -->
</div>

<div ng-show='businessHours'>
   <!-- error message saying that the user must come back at the time left -->
</div>

BUT keep in mind it's very easy to hack. If user simply change hour on his machine the application won't show error message. So this check should be moved to server side code. But it's not Angular matter anymore.

Comments