Skip to content

Instantly share code, notes, and snippets.

@fupslot
Created April 24, 2013 08:59
Show Gist options
  • Save fupslot/5450755 to your computer and use it in GitHub Desktop.
Save fupslot/5450755 to your computer and use it in GitHub Desktop.
// returns total amount of the days that passed since Jan 1 2000
function getDay(aDate) {
if (typeof aDate === "string") {
aDate = new Date(aDate);
}
var zeroYear = 2000;
var totalYears = aDate.getFullYear() - zeroYear;
// calculate all days that passed
// since Jan 1 2000 till current time
var leapYear;
var daysInMonth;
var day = 0;
for (var j = 0; j < totalYears + 1; j++) {
// is current year leap
leapYear = ((j + zeroYear) % 4 == 0);
// total month in current year
var totalMonth = (j == totalYears) ? aDate.getMonth() : 11;
for (var i = 1, l = totalMonth + 1; i <= l; i++) {
if ((j == totalYears) && (i == l)) {
day += aDate.getDate();
} else {
day += getDaysByMonth(i, leapYear);
}
}
}
return day;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment