Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CoffeeScript calculates the date of Easter


May 09, 2021 CoffeeScript


Table of contents


Calculate the date of Easter

Problem

You need to find the month and date of Easter in the year given.

Solution

The following function returns an array with two elements: the month of Easter (1-12) and the date. I f no parameters are given, the result is the current year. This is implemented in CoffeeScript's anonymous Gregorian algorithm.

gregorianEaster = (year = (new Date).getFullYear()) ->
  a = year % 19
  b = ~~(year / 100)
  c = year % 100
  d = ~~(b / 4)
  e = b % 4
  f = ~~((b + 8) / 25)
  g = ~~((b - f + 1) / 3)
  h = (19 * a + b - d - g + 15) % 30
  i = ~~(c / 4)
  k = c % 4
  l = (32 + 2 * e + 2 * i - h - k) % 7
  m = ~~((a + 11 * h + 22 * l) / 451)
  n = h + l - 7 * m + 114
  month = ~~(n / 31)
  day = (n % 31) + 1
  [month, day]

Discuss

The month in Javascript is 0-11. G etMonth() looks for the number 2 that will be returned in March, and this function will return 3. I f you want this function to be consistent, you can modify this function.

The function ~~ Math.floor() with a sign of .

gregorianEaster()    # => [4, 24] (April 24th in 2011)
gregorianEaster 1972 # => [4, 2]