0

I want to print the number of days from the selected date. Instead of printing the number of days from the current date to the previous date, it prints results it in milliseconds. snippet below

 $scope.countDays= function( d2) {
              var diff = Math.abs(new Date().getTime() - d2.getTime());
              alert(diff); //prints milliseconds
              return diff / (1000 * 60 * 60 * 24);
            };

<input ng-model="dateSelection" ng-change="countDays(dateSelection)">

how can I print the exact days difference in actual integer and disable future date selection

user10445503
  • 165
  • 1
  • 12
  • it's still a duplicate of that question, though. The answer is already there for you to fix what you've written. – ADyson Oct 25 '18 at 08:48
  • @ADyson I have re-editted and reworded my problem/challenge – user10445503 Oct 25 '18 at 09:41
  • Ok but it's still a duplicate of about a dozen other questions...you just need to do some simple googling. Here's a good answer about getting the difference between different dates: https://stackoverflow.com/a/1968175/5947043 – ADyson Oct 25 '18 at 09:53
  • the SO link solutions is similar to what I did and getting milliseconds – user10445503 Oct 25 '18 at 09:59
  • okay.... looking deeper I used Math.round(...) and solved my problem. Thanks for giving it to me the hard way – user10445503 Oct 25 '18 at 10:09
  • 1
    please upvote me... thanks. It was a honest attempt from the mindset of a learner – user10445503 Oct 25 '18 at 10:10
  • you weren't getting milliseconds at all, you were just getting a number of days with some decimal points. As you say, rounding it will produce a neater end result, but it's still basically the same as all those other questions and answers. How to round a number is kind of a separate issue. – ADyson Oct 25 '18 at 10:11
  • okay, thanks for the learning curve – user10445503 Oct 25 '18 at 10:15

1 Answers1

0

It should be

$scope.resumption = new Date(new Date().setDate(new Date().getDate() - 30));
Mahbub Moon
  • 491
  • 2
  • 8
  • if you want to deal date , plesae use moment.js, [enter link description here][1], moment.substract(30, 'days').calender() // 30 days before now [1]: http://momentjs.cn/ – bolt Oct 25 '18 at 08:53