-3

I am using the ionic framework and I am trying to calculate the number of days between today and Sun Apr 18 2017 15:14:36 GMT+0800 (MYT)

and today :Thu Mar 09 2017 17:52:48 GMT+0800 (MYT)

it's showing that there are 44 days but I know it's 40 days. Can someone help me fix my code

$scope.daysLeft=Math.floor(Math.abs(newDate - new Date())/86400000);

noor
  • 651
  • 1
  • 8
  • 19
  • 1
    Possible duplicate of [How do I get the number of days between two dates in JavaScript?](http://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – Martin Schneider Mar 09 '17 at 09:24
  • how come it is 40 days? march-21 days left and april 23 days.. – varun aaruru Mar 09 '17 at 09:40
  • @MartinSchneider Im not sure what I'm doing wrong. And I did refer to that post but it's giving me the wrong day number. – noor Mar 09 '17 at 09:42
  • `Math.round((new Date(2017,3,18)-new Date(2017, 2, 09))/(1000*60*60*24)) -> 40`, the linked method is correct – Martin Schneider Mar 09 '17 at 11:09

1 Answers1

1

You are wrong and the result is actually ok from your program. There are 44 days between today and the selected date.

Just try here if you don't believe it.

Community
  • 1
  • 1
rakwaht
  • 3,666
  • 3
  • 28
  • 45
  • sorry I entered the wrong date, I''ll update the question. But the date is between today and April 18th. Its showing me 44 days. – noor Mar 09 '17 at 09:40
  • show us the console log..I think you entered the wrong date in code also..@noor – varun aaruru Mar 09 '17 at 09:47
  • my console log produces this for new Date() :Thu Mar 09 2017 17:52:48 GMT+0800 (MYT) And this for the newDate variable: Tue Apr 18 2017 12:20:02 GMT+0800 (MYT) jobListings.js:185 – noor Mar 09 '17 at 09:53
  • Edit your question don't add here the information requested. – rakwaht Mar 09 '17 at 09:59
  • I did fix the question – noor Mar 09 '17 at 10:32