function Easter(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 = I - 30*Math.floor((I/30));
I = 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 = 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);
return padout(M) + '.' + padout(D);
}
function padout(number) { return (number < 10) ? '0' + number : number; }
function computus( y ) {
var date, a, b, c, m, d;
// Instantiate the date object.
date = new Date;
// Set the timestamp to midnight.
date.setHours( 0, 0, 0, 0 );
// Set the year.
date.setFullYear( y );
// Find the golden number.
a = y % 19;
// Choose which version of the algorithm to use based on the given year.
b = ( 2200 <= y && y <= 2299 ) ?
( ( 11 * a ) + 4 ) % 30 :
( ( 11 * a ) + 5 ) % 30;
// Determine whether or not to compensate for the previous step.
c = ( ( b === 0 ) || ( b === 1 && a > 10 ) ) ?
( b + 1 ) :
b;
// Use c first to find the month: April or March.
m = ( 1 <= c && c <= 19 ) ? 3 : 2;
// Then use c to find the full moon after the northward equinox.
d = ( 50 - c ) % 31;
// Mark the date of that full moon—the "Paschal" full moon.
date.setMonth( m, d );
// Count forward the number of days until the following Sunday (Easter).
date.setMonth( m, d + ( 7 - date.getDay() ) );
// Gregorian Western Easter Sunday
return date;
}
例如:
console.log( computus( 2016 ) ); // Date 2016-03-27T05:00:00.000Z
function isEasterSunday() {
var curdate = new Date();
var year = curdate.getFullYear();
var a = year % 19;
var b = Math.floor(year / 100);
var c = year % 100;
var d = Math.floor(b / 4);
var e = b % 4;
var f = Math.floor((b + 8) / 25);
var g = Math.floor((b - f + 1) / 3);
var h = (19 * a + b - d - g + 15) % 30;
var i = Math.floor(c / 4);
var k = c % 4;
var l = (32 + 2 * e + 2 * i - h - k) % 7;
var m = Math.floor((a + 11 * h + 22 * l) / 451);
var n0 = (h + l + 7 * m + 114)
var n = Math.floor(n0 / 31) - 1;
var p = n0 % 31 + 1;
var date = new Date(year,n,p);
return ((curdate.getMonth() == date.getMonth())&&(curdate.getDate() == date.getDate()));
}
function isEaster(dateObject) {
const date = dateObject ?? new Date();
const year = date.getFullYear();
// Calculate the date of good friday for the current year
const a = year % 19;
const b = Math.floor(year / 100);
const c = year % 100;
const d = Math.floor(b / 4);
const e = b % 4;
const f = Math.floor((b + 8) / 25);
const g = Math.floor((b - f + 1) / 3);
const h = (19 * a + b - d - g + 15) % 30;
const i = Math.floor(c / 4);
const k = c % 4;
const l = (32 + 2 * e + 2 * i - h - k) % 7;
const m = Math.floor((a + 11 * h + 22 * l) / 451);
const n = Math.floor((h + l - 7 * m + 114) / 31) - 1;
const p = (h + l - 7 * m + 114) % 31;
// Calculate the easter dates
const maundyThursday = new Date(year, n, p - 1);
const goodFriday = new Date(year, n, p);
const holySaturday = new Date(year, n, p + 1);
const easterSunday = new Date(year, n, p + 2);
const easterMonday = new Date(year, n, p + 3);
return ([maundyThursday, goodFriday, holySaturday, easterSunday, easterMonday].map(elem => {
return elem.toISOString().split('T')[0];
}).indexOf(date.toISOString().split('T')[0]) > -1);
}
6条答案
按热度按时间q3aa05251#
根据this:
示例用法:-
piok6c0g2#
这里有一种替代方法,它基于R. Sivaraman从最初由J. Meeus为儒略历开发的算法(参见https://en.wikipedia.org/wiki/Computus)改编为格里高利历的算法。
这似乎是一个更优雅和直观的解决方案比高斯算法已经提到.至少它剃掉了几个步骤(只有5个总数加上JS date methods)和使用更少的变量(只有4个总数加上月份,日期和年份).
例如:
ht4b089n3#
使用“Laurent Longre”算法
下面是基于Laurent Longre算法。
它从1583年(公历开始)开始工作。
输出是
[year, month, day]
形式的数组。s6fujrry4#
如果你想知道现在是否是复活节(星期天),你可以使用这个函数.(这是我搜索的):
gfttwv5a5#
不知道其他答案有多准确,但this answer/package是一个js端口,来自Moontool for Windows的公式,这是可信的。
如果你只是想要普通的js,你可以复制/粘贴这段代码。你会想要使用
getEaster(year)
,即.getEaster(2020)
来返回当年复活节的Date
。即
getEaster(2020); // -> Sun Apr 12 2020
fkaflof66#
检查给定的日期是否在复活节。如果没有给定日期,则使用当前日期。🙂