1

I'm trying to figure out how to subtract two different dates to get the remainder. Based on my Google searches, this seems like it should be pretty straight forward, but my code just isn't working as expected.

const options = { year: 'numeric', month: 'numeric', day: 'numeric' };
let today = new Date();
today = today.toLocaleDateString('en-US', options); // '2/20/2019'
dueDate = new Date(dueDate[0]);
dueDate = dueDate.toLocaleDateString('en-US', options); // '12/15/2019'
daysLeft = today.setDate(today.setDate() - dueDate); // Being declared as a let outside the scope block

The error message I'm receiving is: Uncaught (in promise) TypeError: today.setDate is not a function

enter image description here

UPDATE:

The possible duplicate answer almost helped me, but it doesn't account for the years so 2/20/2019 - 2/1/2001 is outputting 19, which is incorrect.

pingeyeg
  • 632
  • 2
  • 6
  • 23
  • 2
    [MomentJs](https://momentjs.com/) is your friend; see [difference](https://momentjs.com/docs/#/displaying/difference/) – Tushar Walzade Feb 20 '19 at 13:11
  • @TusharWalzade MomentJS seems like a hefty library for such a small need. I could certainly be wrong though. – pingeyeg Feb 20 '19 at 13:12
  • 1
    [Have you tried this?](https://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript) – Tholle Feb 20 '19 at 13:14
  • Possible duplicate of [Get difference between 2 dates in JavaScript?](https://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript) – espindolaa Feb 20 '19 at 13:23
  • 1
    @Tholle dude, that certainly helped me out. I'm surprised that didn't come up in my searches, but regardless, I still have an issue. It seems it's only looking at the day number and not accounting for the year. So, the total is displaying 19, but obviously that can't be true since the years don't line up. – pingeyeg Feb 20 '19 at 13:25
  • @LucasEspindola that answer certainly helped me out, but it's not accounting for the years so doesn't fully answer my issue. – pingeyeg Feb 20 '19 at 13:26

3 Answers3

3

You can use straight math.

let today = new Date();
let dueDate = new Date('12/15/2019');
let difference = Math.abs(Math.round((today.getTime()-dueDate.getTime())/1000/24/60/60));
console.log(difference);

This way we get the difference in milliseconds, and divide by 1000 to get seconds, by 60 to get minutes, by 60 again to get hours and by 24 finally to get days difference.

Tushar Walzade
  • 3,737
  • 4
  • 33
  • 56
Vladimir Bogomolov
  • 1,754
  • 13
  • 17
1

Well the main problem is you're parsing the date today to a string and then you're calling a method on it, which is naturally gonna fail. You should assign the value of today.toLocaleDateString('en-US', options), which is a string, to another variable and use the method on the variable that actually has the Date object inside. This is assuming the rest of the code is fine.

  • Interesting, that was an answer I found through Google, which based on what each function's objective, seemed legit, but I haven't dealt with dates in quite some time. – pingeyeg Feb 20 '19 at 13:23
1

MomentJS is your friend! You could simply use a diff() as follows -

moment.locale('en-US');   // setting locale
var today = moment();   // current date
var dueDate = moment('12/15/2019', "MM/DD/YYYY"); // due date
console.log(Math.abs(dueDate.diff(today, 'days'))); // difference in days, possible values are months, years...
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
  • Note: Math.abs() avoids negative values if you provide a historical date!
Tushar Walzade
  • 3,737
  • 4
  • 33
  • 56
  • 1
    After doing some reading on MomentJS, it turned out to work very well for me so thanks for this snippet of code. I had to refactor a little, but wound up being worth it in the end. – pingeyeg Feb 20 '19 at 15:38