javascriptdatetimeunix-timestampcomputus

easter_date() in JavaScript


I'm making a calendar generator in JavaScript. I need the Unix Timestamp for easter day midnight, for a given year. How can I do that (in JavaScript)?

PHP's function can be found here.


Solution

  • According to this:-

    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; }
    

    Example usage:-

    for (let year = 2015; year < 2030; ++year) {
        console.log(year, Easter(year));
    }
    

    Output:-

    2015 '04.05'
    2016 '03.27'
    2017 '04.16'
    2018 '04.01'
    2019 '04.21'
    2020 '04.12'
    2021 '04.04'
    2022 '04.17'
    2023 '04.09'
    2024 '03.31'
    2025 '04.20'
    2026 '04.05'
    2027 '03.28'
    2028 '04.16'
    2029 '04.01'