Sudhir
Sudhir

Reputation: 579

disable button based on time using angularjs

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.

Upvotes: 0

Views: 1014

Answers (1)

user6849618
user6849618

Reputation:

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 ?

Upvotes: 1

Related Questions