0

Hi i have to disable button button based on date,my requirement is i will have json that are having values checkinStartDate and checkinEndDate

{
    "checkinStartDate": "10/22/2016 09:10:04 AM",
    "checkinEndDate": "10/22/2016 19:10:04 PM"
}

For example today's date is 10/22/2016 then condition is mentioned below. I have to enable button before 1 hr(10/22/2016 08:10:04 AM) of checkinStartDate and 1 hr(10/22/2016 20:10:04 PM) after checkinEndDate.other than this time button should be in disabled mode Any help will be greatfull.

2
  • @JoaozitoPolo yes it is dynamic Commented Sep 19, 2016 at 16:50
  • Maybe you could use setTimeout() after you check your JSON and get the time the app need to wait. When the setTimeout is trigged you can put your disable variable to true and disable the button. Commented Sep 19, 2016 at 16:55

1 Answer 1

1

suppose your object is assigned to $scope.date Then what you need to do next is.

$scope.checkDate = function () {
  var ONE_HOUR = 60 * 60 * 1000; /* ms */
  var myDate = {
    "checkinStartDate": "10/22/2016 09:10:04 AM",
    "checkinEndDate": "10/22/2016 19:10:04 PM"
  }

  var checkInStart = new Date(myDate.checkinStartDate);
  var checkInEnd = new Date(myDate.checkinEndDate);

  var current = new Date();

  //only enable during this time
  if ((current.getTime() < checkInStart.getTime() + ONE_HOUR) && (current.getTime() + ONE_HOUR > checkInEnd.getTime()))
    return false;


  //otherwhile disable  
  return true;
}

In the view, just call it

<button ng-disabled="checkDate()">View<button>

p/s: it is just the idea, I am not sure my condition is correct because I don't have the environment for testing. the getTime() will return you the number of milliseconds since midnight Jan 1 1970, and a specified date. Then you can use it to compare 1 hour ahead of before. You see what I am saying ?

Sign up to request clarification or add additional context in comments.

3 Comments

HI thanks for your suggestion, but i am getting invalid date for checkInEnd
getting invalid date for checkinEndDate
How is this going Sudhir ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.