Calculate the date of Easter Sunday

后端 未结 6 626
半阙折子戏
半阙折子戏 2020-12-11 16:32

Write a program to compute the date of Easter Sunday. Easter Sunday is the first Sunday after the first full moon of spring. Use the algorithm invented by the mathematician

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-11 16:49

    In case anyone is looking for the updated version (NY Anonymous Gregorian) of the algorithm in Typescript...

    easterDate() {
      var currentYear = new Date().getFullYear();
      var a = Math.floor(currentYear % 19);
      var b = Math.floor(currentYear / 100);
      var c = Math.floor(currentYear % 100);
      var d = Math.floor(b / 4);
      var e = Math.floor(b % 4);
      var f = Math.floor((b + 8) / 25);
      var g = Math.floor((b - f + 1) / 3);
      var h = Math.floor((19 * a + b - d - g + 15) % 30);
      var i = Math.floor(c / 4);
      var k = Math.floor(c % 4);
      var l = Math.floor((32 + 2 * e + 2 * i - h - k) % 7);
      var m = Math.floor((a + 11 * h + 22 * l) / 451);
      var n = Math.floor((h + l - 7 * m + 114) / 31);
      var p = Math.floor(((h + l - 7 * m + 114) % 31) + 1);
      // console.log('a: ' + a + ' b: ' + b + ' c: ' + c + ' d: ' + d + ' e: ' + e);
      // console.log('f: ' + f + ' g: ' + g + ' h: ' + h + ' i: ' + i + ' k: ' + k);
      // console.log('l: ' + l + ' m: ' + m + ' n: ' + n + ' p: ' + p);
      // console.log("In the year " + currentYear + " Easter with fall on day " + p + " of month " + n);
      var month = n.toString();
      while (month.length < 2) month = "0" + month;
      var day = p.toString();
      while (day.length < 2) day = "0" + day;
      var dateString = currentYear.toString() + '-' + month + '-' + day + 'T00:00:00';
      return new Date(dateString);
    }
    

提交回复
热议问题