曜日が変わらないカレンダーについて書きましたが、これをJavaScriptのユーザー定義関数にしてみました。
JavaScriptの通常の日付(日付オブジェクト、ndate)を与えると「ハンキ・ヘンリー・パーマネント・カレンダー」における日付(文字列、pdate)に変換します。
返る値は「2012-1-19」という形式です。
使い道が思い当たりませんが。
function date2hhpc(ndate) {
var di, ldi, preldi, y, ydi, isext, sdi, pdate;
//日のインデックス。2012-01-01(初日)を0日目とする
di = (ndate - (new Date(2012, 0, 1))) / 86400000;
//対応しているのは2012年から2167年まで
if(di < 0 || di > 20453) {
return "error";
}
//大晦日のインデックス
ldi = -1;
year = 2011;
while(di > ldi) {
year++;
//前年の大晦日
preldi = ldi;
isext = String(year).match(/(2015|2020|2026|2032|2037|2043|2048|2054|2060|2065|2071|2076|2082|2088|2093|2099|2105|2111|2116|2122|2128|2133|2139|2144|2150|2156|2161|2167)/) ? 1 : 0;
ldi += 364 + isext * 7;
}
ydi = di - preldi - 1;
if(ydi > 363) {
pdate = "E/" + (ydi - 363);
} else {
sdi = ydi % 91;
x = Math.floor(sdi / 30) + 1;
x = (x > 3) ? 3 : x;
pdate = (Math.floor(ydi / 91) * 3 + x) + "-" + (sdi - 30 * (x - 1) + 1);
}
return year + "-" + pdate;
}
var di, ldi, preldi, y, ydi, isext, sdi, pdate;
//日のインデックス。2012-01-01(初日)を0日目とする
di = (ndate - (new Date(2012, 0, 1))) / 86400000;
//対応しているのは2012年から2167年まで
if(di < 0 || di > 20453) {
return "error";
}
//大晦日のインデックス
ldi = -1;
year = 2011;
while(di > ldi) {
year++;
//前年の大晦日
preldi = ldi;
isext = String(year).match(/(2015|2020|2026|2032|2037|2043|2048|2054|2060|2065|2071|2076|2082|2088|2093|2099|2105|2111|2116|2122|2128|2133|2139|2144|2150|2156|2161|2167)/) ? 1 : 0;
ldi += 364 + isext * 7;
}
ydi = di - preldi - 1;
if(ydi > 363) {
pdate = "E/" + (ydi - 363);
} else {
sdi = ydi % 91;
x = Math.floor(sdi / 30) + 1;
x = (x > 3) ? 3 : x;
pdate = (Math.floor(ydi / 91) * 3 + x) + "-" + (sdi - 30 * (x - 1) + 1);
}
return year + "-" + pdate;
}
コメント