Skip to content

Instantly share code, notes, and snippets.

@datatypevoid
Forked from endel/getMoonPhase.js
Last active September 11, 2022 15:42
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save datatypevoid/f4dd1f6439feaa588bb2aaf4f8f4361f to your computer and use it in GitHub Desktop.
Save datatypevoid/f4dd1f6439feaa588bb2aaf4f8f4361f to your computer and use it in GitHub Desktop.
Get moon phase by Date, written in TypeScript. Accurate from year 1900 to 2199 inclusive.
export { EMoonPhase, EMoonPhaseName, MOON_PHASE_NAMES, moonPhase }
const { round, trunc: truncate } = Math
enum EMoonPhase {
New = 0,
WaxingCrescent,
QuarterMoon,
WaxingGibbous,
Full,
WaningGibbous,
LastQuarter,
WaningCrescent,
COUNT = 8
}
enum EMoonPhaseName {
New = 'New',
WaxingCrescent = 'Waxing Crescent',
QuarterMoon = 'Quarter Moon',
WaxingGibbous = 'Waxing Gibbous',
Full = 'Full',
WaningGibbous = 'Waning Gibbous',
LastQuarter = 'Last Quarter',
WaningCrescent = 'Waning Crescent',
COUNT = 'COUNT'
}
const MOON_PHASE_NAMES: EMoonPhaseName[] = [ // Look-up table.
EMoonPhaseName.New,
EMoonPhaseName.WaxingCrescent,
EMoonPhaseName.QuarterMoon,
EMoonPhaseName.WaxingGibbous,
EMoonPhaseName.Full,
EMoonPhaseName.WaningGibbous,
EMoonPhaseName.LastQuarter,
EMoonPhaseName.WaningCrescent,
EMoonPhaseName.COUNT
]
// Reference: http://individual.utoronto.ca/kalendis/lunar/#FALC
// Also known as a synodic month.
// An average synodic month takes 29 days, 12 hours, 44 minutes, 3 seconds.
const LUNAR_CYCLE = 29.53058770576
const DAYS_PER_YEAR = 365.25
const DAYS_PER_MONTH = 30.6
// Number of days since known new moon on `1900-01-01`.
const DAYS_SINCE_NEW_MOON_1900_01_01 = 694039.09
interface IResult { name: EMoonPhaseName, phase: EMoonPhase }
// Ported from `http://www.voidware.com/moon_phase.htm`.
function moonPhase (date: Date = new Date()): IResult {
// let year = date.getYear()
let year: number = date.getFullYear()
let month: number = date.getMonth()
const day: number = date.getDay()
if (month < 3) {
year--
month += 12
}
month++
let totalDaysElapsed: number = DAYS_PER_YEAR
* year
+ DAYS_PER_MONTH
* month
+ day
- DAYS_SINCE_NEW_MOON_1900_01_01
totalDaysElapsed /= LUNAR_CYCLE // Divide by the lunar cycle.
let phase: number = truncate(totalDaysElapsed)
/*
Subtract integer part to leave fractional part of original
`totalDaysElapsed`.
*/
totalDaysElapsed -= phase
// Scale fraction from `0-8`.
phase = round(totalDaysElapsed * 8)
phase = phase & 7 // `0` and `8` are the same so turn `8` into `0`.
if (phase >= EMoonPhase.COUNT || phase < EMoonPhase.New)
throw new Error(`Invalid moon phase: ${phase}`)
return { phase, name: MOON_PHASE_NAMES[phase] }
}
@whiteangelnoor
Copy link

how can have contact with you?

@EricHech
Copy link

sent you a repo collab

@whiteangelnoor
Copy link

Yes, I think that wouldn't be too hard to add with some investigation into calculating lunar degrees.

how contact with you

@EricHech
Copy link

You're cluttering up the gist man. I sent you a repo collaboration invite so you can contact me through the codebase.

@whiteangelnoor
Copy link

You're cluttering up the gist man. I sent you a repo collaboration invite so you can contact me through the codebase.

i didnt have and received any thing

@whiteangelnoor
Copy link

i accept it.now what?

@EricHech
Copy link

This is my last message here. Check the README. My email is there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment