moment-biz
Version:
Handle business days & weekends over multiple years
48 lines (38 loc) • 1.76 kB
JavaScript
;
const moment = require('moment');
/*
Easter Date Function for JavaScript implemented by Furgelnod (http://furgelnod.com)
Using algorithm published at The Date of Easter (on aa.usno.navy.mil, Oct 2007)
(https://web.archive.org/web/20071015045929/http://aa.usno.navy.mil/faq/docs/easter.php)
The algorithm is credited to J.-M. Oudin (1940) and is reprinted in the
Explanatory Supplement to the Astronomical Almanac, ed. P. K. Seidelmann (1992).
See Chapter 12, "Calendars", by L. E. Doggett.
*/
// Takes a given year (y) then returns a moment object of Easter Sunday
const easter = module.exports = moment.easter = function easter(y = moment().year()) {
if (!easter.cache)
easter.cache = {};
if (isNaN(+y))
throw new TypeError('Value must be a number.');
else if (y > 275760 || y < -271820)
throw new RangeError('Value must be between -271820 and 275760 due to technical limitations of Date constructor.');
y = Math.floor(y);
if (y in easter.cache)
return moment(easter.cache[y]);
var c = Math.floor(y / 100);
var n = y - 19 * Math.floor(y / 19);
var k = Math.floor((c - 17) / 25);
var i = c - Math.floor(c / 4) - Math.floor((c - k) / 3) + 19 * n + 15;
i -= 30 * Math.floor(i / 30);
i -= Math.floor(i / 28) * (1 - Math.floor(i / 28) * Math.floor(29 / (i + 1)) * Math.floor((21 - n) / 11));
var j = y + Math.floor(y / 4) + i + 2 - c + Math.floor(c / 4);
j -= 7 * Math.floor(j / 7);
var l = i - j;
var m = 3 + Math.floor((l + 40) / 44);
var d = l + 28 - 31 * Math.floor(m / 4);
easter.cache[y] = moment({year: y, month: m - 1, day: d});
return moment(easter.cache[y]);
};
moment.fn.isEaster = function() {
return moment.easter(this.year()).dayOfYear() === this.dayOfYear();
};