Skip to content

Instantly share code, notes, and snippets.

@timrwood
Created May 25, 2012 19:08
Show Gist options
  • Save timrwood/2789940 to your computer and use it in GitHub Desktop.
Save timrwood/2789940 to your computer and use it in GitHub Desktop.
Year difference
var now = new Date();
var birth = new Date(1986, 6, 10); // remember that we're using zero indexed months...
var yearDiff = now.getFullYear() - birth.getFullYear();
var monthDiff = now.getMonth() - birth.getMonth();
var dayDiff = now.getDate() - birth.getDate();
var hourDiff = now.getHours() - birth.getHours();
var minuteDiff = now.getMinutes() - birth.getMinutes();
var secondDiff = now.getSeconds() - birth.getSeconds();
var millisecondDiff = now.getMilliseconds() - birth.getMilliseconds();
yearDiff += monthDiff / 12;
yearDiff += dayDiff / (12 * 30);
yearDiff += hourDiff / (12 * 30 * 24);
yearDiff += minuteDiff / (12 * 30 * 24 * 60);
yearDiff += secondDiff / (12 * 30 * 24 * 60 * 60);
yearDiff += millisecondDiff / (12 * 30 * 24 * 60 * 60 * 1000);
console.log(yearDiff);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment