Skip to content

Instantly share code, notes, and snippets.

@petercunha
Created October 18, 2024 23:38
Show Gist options
  • Save petercunha/00a767c02e9b028c4d3506cd81763447 to your computer and use it in GitHub Desktop.
Save petercunha/00a767c02e9b028c4d3506cd81763447 to your computer and use it in GitHub Desktop.
Find the next 'n' Friday the 13ths in October that have a full moon
// Constants for the calculations
const SYNODIC_MONTH = 29.53058867; // Average lunar cycle in days (new moon to new moon)
const KNOWN_FULL_MOON = new Date(Date.UTC(2000, 0, 21, 4, 44)); // Known full moon date on 2000-01-21 04:44 UTC
/**
* Returns the number of days from the known full moon date to the input date.
* @param {Date} date
* @returns {number} Number of days since known full moon
*/
function daysSince(date) {
const msPerDay = 1000 * 60 * 60 * 24; // Convert milliseconds to days
const diff = date - KNOWN_FULL_MOON; // Difference from the known full moon
return diff / msPerDay; // Convert difference to days
}
/**
* Determines if a given date has a full moon based on the full moon cycle.
* @param {Date} date - The date to check
* @returns {boolean} True if the date is within about a day of a full moon
*/
function isFullMoon(date) {
const daysSinceKnownFullMoon = daysSince(date);
const synodicAge = daysSinceKnownFullMoon % SYNODIC_MONTH; // Age of the moon in terms of phase
const distanceToFullMoon = Math.min(synodicAge, SYNODIC_MONTH - synodicAge); // Nearest distance to full moon
return distanceToFullMoon < 1.0; // Close to a full moon within 1 day
}
/**
* Function that finds Friday the 13ths in October with full moons
*/
function findFridayThe13thsWithFullMoon(n) {
const results = [];
let year = new Date().getFullYear(); // Start from the current year
while (results.length < n) {
for (let month = 0; month < 12; month++) {
const day = new Date(year, month, 13);
// Check for Friday the 13th in October
if (day.getDay() === 5 && day.getMonth() === 9) { // 5 is Friday, 9 is October
if (isFullMoon(day)) {
results.push(day);
}
}
}
year++;
}
return results;
}
// Find the next 3 Friday the 13ths in October with full moons
const result = findFridayThe13thsWithFullMoon(100);
result.forEach(date => {
console.log(`Found a Friday the 13th with full moon on: ${date.toDateString()}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment