1

I am stuck with one condition where I have to change the path based on time.

I will have two date variables, CheckInStartDate and CheckInEndDate, which will be coming from API. Current time is system date time.

Path has to change in two conditions.

  1. CheckInStartDate minus 1 hour of current time
  2. CheckInEndDate plus 1 hour of current time.

This is the code I currently have.

$scope.checkIn = function() {
    var ONE_HOUR = 60 * 60 * 1000; /* ms */
    $scope.checkInStartDate=  01/16/2017 09:06:00 AM;
    $scope.checkInEndDate=  01/16/2017 11:06:00 AM;
    var checkInStartDate=$scope.checkInStartDate;
    var checkInEndDate=$scope.checkInEndDate;
    var currentDate = new Date();
    var checkinStartDate=new Date(checkInStartDate);
    var checkinEndDate = new Date(checkInEndDate);

    if ((checkinStartDate.getTime()) > (currentDate.getTime() - ONE_HOUR) ||
            (checkinEndDate.getTime()) < (currentDate.getTime() + ONE_HOUR)) {
        $location.path('/checkIn')
    }
    else{
        alert("cannot checkin");
    }
}
Just a student
  • 10,560
  • 2
  • 41
  • 69
Sudhir MN
  • 125
  • 8
  • 2
    What is your question exactly? Your code looks mostly fine to me, except that initially setting `$scope.checkInStartDate` the way you do is a syntax error (and similarly for the end date). – Just a student Jan 16 '17 at 09:38
  • You forgot quotes around the date-time in lines 3 and 4. – Hubert Grzeskowiak Jan 16 '17 at 09:38
  • Can you not use https://momentjs.com/? it would make modifying the dates easier? – Qpirate Jan 16 '17 at 09:39
  • $scope.checkInStartDate and end date i will get from db just i am showing the axample – Sudhir MN Jan 16 '17 at 09:40
  • 1
    Are you sure the logical ´OR´ is correct and that it shouldn't be an ´AND´? – Hubert Grzeskowiak Jan 16 '17 at 09:42
  • @Justastudent i have to change the path where if $scope.checkInStartDate is 9:06 am and current time is 8:06am and similarly for $scope.checkInEndDate is 11:06 and current time is before 12:06 the path has to change – Sudhir MN Jan 16 '17 at 09:43
  • That is almost what your code is doing. If I understand you correctly, you may want an AND instead of an OR as @HubertGrzeskowiak suggested. If not: what do you want to happen and what is actually happening? – Just a student Jan 16 '17 at 09:46
  • Something I thought of just now: your code might not work correctly when the API is returning times in a different timezone than your system is in. If that is the case, you have to explicitly set the timezone when constructing `Date` objects. – Just a student Jan 16 '17 at 09:57
  • @Justastudent i have changed OR to AND but also it is going to else condition. I have given checkInStartDate as 1pm and checkInEndDate as 5 pm it is not working – Sudhir MN Jan 16 '17 at 09:57
  • @Justastudent How to set timezone objects – Sudhir MN Jan 16 '17 at 09:58
  • There are different ways, you may want to read [this question](http://stackoverflow.com/questions/20834411/how-do-i-specify-the-time-zone-when-creating-a-javascript-date) and the answers to it. You can check if this is the problem using `console.log` on all three dates. – Just a student Jan 16 '17 at 10:00
  • @Justastudent iam getting dates like Mon Jan 16 2017 06:22:00 GMT-0800 (Pacific Standard Time) in pst times. I have changed my sytem ttime also to PST my current time is Mon Jan 16 2017 02:27:55 GMT-0800 (Pacific Standard Time) – Sudhir MN Jan 16 '17 at 10:29
  • @HubertGrzeskowiak i have changed OR to AND but it is not working can u please check my code once – Sudhir MN Jan 16 '17 at 10:30
  • 1
    We cannot check anything unless you tell us what you are trying to achieve. Please define the expected behaviour. – Hubert Grzeskowiak Jan 16 '17 at 10:31
  • @HubertGrzeskowiak i have to change my path based on the below condition if any one condition is true path has to change.I will get two dates checkInStart and checkInEnd . If current time is 5Am and checkInStart time is 4 Am and checkInEnd time is 8Am then condition is true. this condition must be true till current time is from 3AM to 9AM. – Sudhir MN Jan 16 '17 at 10:37
  • @SudhirMN this means that not "any condition" must be true but both at the same time. – Hubert Grzeskowiak Jan 16 '17 at 10:39
  • @HubertGrzeskowiak must be true between 4am to 9am. if Checkinstart is 3Am and CheckinEnd is 8 am – Sudhir MN Jan 16 '17 at 10:41
  • If @HubertGrzeskowiak's answer is not what you are looking for, please put some testcases in [this Fiddle](https://jsfiddle.net/kovxxp5n/) to show us what you mean. – Just a student Jan 16 '17 at 10:49

1 Answers1

1

If I got your question correctly, you want to allow check-in in the time period between the following two times:

  • CheckInStartDate minus 1 hour
  • CheckInEndDate plus 1 hour

Let's call the first point s and the second point e. The current time is c. In this case your condition should be true when

s < c < e

In JavaScript this is equivalent:

var s = new Date($scope.checkInStartDate).getTime() - ONE_HOUR;
var e = new Date($scope.checkInEndDate).getTime() + ONE_HOUR;
var c = new Date().getTime();
if (s < c && c < e) {
    // check-in allowed
} else {
    // not allowed
}
Hubert Grzeskowiak
  • 15,137
  • 5
  • 57
  • 74