0

I am fetching the date from my Django backend which comes like this: 2016-03-31 In my angular controller I want to compare it with today's date and if they are similar I want to deactivate a button.

I tried new Date() which gives me something like Thu Mar 31 2016 08:59:01 GMT+0200 (W. Europe Daylight Time)

How can these two dates be compared to achieve my goal?

Thinker
  • 5,326
  • 13
  • 61
  • 137

2 Answers2

1

ehhhhh... i think currently we could only do a manual formatting

see this one: How to format a JavaScript date

For your reference, below is what i did though:

$scope.formatDate = function(date){
    var newDate = new Date(date);


    var year = newDate.getFullYear();
    var month = (newDate.getMonth() + 1).toString(); //add 1 as Jan is '0'
    var day = newDate.getDate().toString();

    month = month.length > 1? month: '0'+month;
    day = day.length > 1? day: '0'+day;

    $scope.date = day + '/' + month + '/' + year;
}
Community
  • 1
  • 1
Chen Sturmweizen
  • 639
  • 6
  • 12
0

If you want to compare the date with now you can do the following:

var now = new Date().getTime();
var compareDate = new Date('2016-03-31').getTime();

if(now > compareDate){
    //greater
}else if(now < compareDate){
    //less
}else{
   //equal
}

Just add what you need to the scope and then you could do something like:

ng-disabled="compareDate === today"

Justin Ober
  • 839
  • 6
  • 14