Skip to content

Instantly share code, notes, and snippets.

@derozic
Last active April 10, 2018 20:50
Show Gist options
  • Save derozic/1c53a8c518d5866a770e7fc52df62c4f to your computer and use it in GitHub Desktop.
Save derozic/1c53a8c518d5866a770e7fc52df62c4f to your computer and use it in GitHub Desktop.
// Define two dates to compare to get to the number of days
// This example is the number of days a woman has to work
// before then making the same pay as a man in the US https://www.pay-equity.org/day.html
var date1 = '2018-04-10';
var date2 = '2018-01-01';
// First we split the values to arrays date1[0] is the year, [1] the month and [2] the day
date1 = date1.split('-');
date2 = date2.split('-');
// Now convert the array to a Date object, which has several helpful methods
date1 = new Date(date1[0], date1[1], date1[2]);
date2 = new Date(date2[0], date2[1], date2[2]);
// We now use the getTime() method and get the unixtime (in milliseconds, but we want seconds, therefore we divide it through 1000)
date1_unixtime = parseInt(date1.getTime() / 1000);
date2_unixtime = parseInt(date2.getTime() / 1000);
// This is the calculated difference in seconds
var timeDifference = date2_unixtime - date1_unixtime;
// in Hours
var timeDifferenceInHours = timeDifference / 60 / 60;
// and finaly, in days :)
var timeDifferenceInDays = timeDifferenceInHours / 24;
// make it great by putting it in an alert
// keep things positive
alert(-timeDifferenceInDays);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment