# Calculating days between dates in JavaScript

/I'm working on a React/Redux app that pulls your friends' upcoming birthdays from Facebook, and recommends gifts for them based on their profile data. Our team finished the MVP (aka, "minimally viable product") this week, and one of the last TODOs was to render a user's friends list by upcoming birthday. We wanted the soonest birthday to show up first.

I took on the task, and had a lot of fun understanding the problem, and discovering edge cases. I also learned more about JavaScript's Date object, which is very handy.

**My Approach**

- Get today's date, which you can do with JavaScript's
`Date.getDate()`

method. This will return the number of milliseconds between January 1, 1970 and today (long story). Add 1 to the month because JavaScript starts counting at 0, and you want to set January to 1, not 0. - Get friend's birthday from Facebook, also in milliseconds
- Change friend's birth year to the current year, since their upcoming birthday is what you care about
- Subtract today's date from friend's birthdate. If result is negative, that means that your friend's birthday has already passed, so add 365 to get the number of days until their next birthday (TODO: account for leap years)
- Do some work to order your friends by upcoming birthday, soonest first

You can find the commented code on GitHub here. I also added it below, for convenience.

// Redux uses switch statements in its reducers. // It's not so bad once you get used to it, I promise. case FETCH_FRIENDS: var getDaysFromToday = function(birthday) { if(!birthday) { return null; } // create new Date for today var today = new Date(); // add 1 to current month because getMonth returns months starting at 0 var currentMonth = today.getMonth() + 1; var currentYear = today.getFullYear(); var currentDD = today.getDate(); // create new Date for today, reflecting adjusted month today = new Date(currentYear, currentMonth, currentDD); // initialize array to hold friend's 'MM/DD/YY' bday as [ 'MM', 'DD', 'YY' ] var friendBdayArray = birthday.split('/'); var friendBdayMonth = friendBdayArray[0]; var friendBdayDD = friendBdayArray[1]; // create new Date, replacing birth year with current year var friendBday = new Date(currentYear, friendBdayMonth, friendBdayDD); // calculate time between now to friend's day -- getTime() returns time in ms // NOTE: if day is in the past, elapsed will be negative var elapsed = ( friendBday.getTime() - today.getTime() ); // divide by 86,400,000 to convert milliseconds to days var daysFromToday = Math.floor(elapsed / 86400000); // add 365 to any negative daysFromToday value to get days until next year's date daysFromToday > 0 ? daysFromToday = daysFromToday : daysFromToday = daysFromToday + 365; return daysFromToday; }; // append daysFromToday to each friend object and push to friends array var friends = []; action.friends.forEach(function(friend) { friend.daysFromToday = getDaysFromToday(friend.birthday); friends.push(friend); }); // sort friends in array by daysFromToday in descending order friends.sort(function(friendA, friendB){ if (friendA.daysFromToday === null) { return 1; } if (friendB.daysFromToday === null) { return -1; } return friendA.daysFromToday - friendB.daysFromToday; }); // finally, return the updated state (es6 syntax) return Object.assign({}, state, { friends: friends });