2009-08-16 14 views
4

JavaScriptでカレンダージェネレータを作成しています。特定の年の、深夜の深夜にUnixタイムスタンプが必要です。誰が私にそれを(JavaScriptで)行う必要があるか教えてもらえますか?easter_date()in JavaScript

ありがとうございます。

PHPの関数は、ここで見つけることができます:http://www.php.net/easter_date

答えて

14

: -

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

使用例: -

document.write(Easter(1997)); 
+0

おかげで、私は私の他の機能は2016年のUNIXタイムスタンプ –

+0

に変換してみましょう、それはとても正しい2016年のための「復活祭の日曜日」である、「03.27」もたらし2016のために... – Tompi

2

は、彼らが彼らを計算し、JavaScriptで複製する方法を確認するためにPHPのソースコードを見てください?

+0

Omgは、PHPオープンソースですか?さて、私はそれを知りませんでした:p –

+2

参照のための出典: https://github.com/php/php-src/blob/c8aa6f3a9a3d2c114d0c5e0c9fdd0a465dbb54a5/ext/calendar/easter.c – s1lence

1

R. Sivaramanがグレゴリオ暦のためにJ. MeeusがJulianカレンダー用に開発したアルゴリズム(参考文献:https://en.wikipedia.org/wiki/Computus)からのアルゴリズムに基づく別の方法があります。

すでに言及したガウスアルゴリズムよりも、よりエレガントで直感的な解決策のようです。少なくともいくつかのステップ(5つの合計に加えてJS date methods)を削り取り、より少ない変数(合計4プラスプラス月、日付、および年)を使用します。

例えば
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 
+0

なぜ結果には05:00:00.000分?それは時間がUTCであるからですか?これはあなたのアルゴリズムが本当にタイムゾーンに依存しないかどうか心配しています。 – josch