Skip to content

Instantly share code, notes, and snippets.

@James-E-A
Last active June 18, 2025 20:55
Show Gist options
  • Save James-E-A/849acd01187bcf9346bb42231d2f901b to your computer and use it in GitHub Desktop.
Save James-E-A/849acd01187bcf9346bb42231d2f901b to your computer and use it in GitHub Desktop.
Javascript get number of days since start of Gregorian calendar
/*
* Calculate the number of days since the start of the Gregorian calendar.
* @param {Date} [date] - The reference date. Defaults to the current instant.
* @param {boolean} [useLocalTime] - Whether to calculate in local time. WARNING: By default, UTC is used; this will, for instance, give a Tuesday result when run at 10pm on a Monday in the United States.
* @returns {number}
*/
export function gregorianDateInteger(d, useLocalTime) {
if (d === undefined) d = new Date();
if (useLocalTime === undefined) useLocalTime = false;
return Math.floor((d.valueOf() / (1000*60) - (useLocalTime ? d.getTimezoneOffset() : 0)) / (60*24)) + 141428;
}
export default gregorianDateInteger;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment