1: // year年の祝日の配列を返す。
2: function holidays(year){ 3: //日付が同じかどうか判定する関数
4: function isSameDate(date1, date2){ 5: return (
6: date1.getFullYear()==date2.getFullYear()
7: && date1.getMonth()==date2.getMonth()
8: && date1.getDate()==date2.getDate()
9: );
10: }
11: //何年何月何週目の何曜日、のDateを返す。
12: function DateByWeek(year, month, week, day){ //monthはJavaScript月(実際の月-1) 13: return new Date(year, month, 7*(week-1) + (day+7-(new Date(year,month,1)).getDay())%7+1 );
14: }
15: //日付sort用
16: function dateSort(a,b){ 17: return a.getTime()-b.getTime();
18: }
19: //基本祝日リスト
20: var hds = [new Date(year+"/1/1")]; //元日
21: hds.push(DateByWeek(year, 1-1, 2, 1)); //成人の日
22: hds.push(new Date(year+"/2/11")); //建国記念の日
23: hds.push(new Date(year+"/3/"+(Math.floor(20.8431+0.242194*(year-1980)-Math.floor((year-1980)/4))))); //春分の日(1980〜2099)
24: hds.push(new Date(year+"/4/29")); //昭和の日
25: hds.push(new Date(year+"/5/3")); //憲法記念日
26: hds.push(new Date(year+"/5/4")); //みどりの日
27: hds.push(new Date(year+"/5/5")); //こどもの日
28: hds.push(DateByWeek(year, 7-1, 3, 1)); //海の日
29: hds.push(DateByWeek(year, 9-1, 3, 1)); //敬老の日
30: hds.push(new Date(year+"/9/"+(Math.floor(23.2488+0.242194*(year-1980)-Math.floor((year-1980)/4))))); //秋分の日(1980〜2099)
31: hds.push(DateByWeek(year, 10-1, 2, 1)); //体育の日
32: hds.push(new Date(year+"/11/3")); //文化の日
33: hds.push(new Date(year+"/11/23")); //勤労感謝の日
34: hds.push(new Date(year+"/12/23")); //天皇誕生日
35: //振替休日探し
36: function isHoliday(d){ 37: for(var i=0; i<hds.length; i++)
38: if(isSameDate(d, hds[i])) return true;
39: return false;
40: }
41: for(var i=0; i<hds.length; i++){ 42: if(hds[i].getDay()==0){ //日曜日 43: nd = new Date(hds[i].getFullYear(), hds[i].getMonth(), hds[i].getDate()+1); //翌日
44: while(isHoliday(nd))
45: nd.setDate(nd.getDate()+1);
46: hds.push(nd);
47: }
48: }
49: hds.sort(dateSort);
50: //祝日に挟まれたら連休にする
51: for(var i=1; i<hds.length; i++){ 52: var ototoi = new Date(hds[i]); //ototoiは2日前
53: ototoi.setDate(ototoi.getDate()-2);
54: if(isSameDate( ototoi, hds[i-1])){ 55: ototoi.setDate(ototoi.getDate()+1)
56: hds.push(ototoi);
57: }
58: }
59: hds.sort(dateSort);
60:
61: return hds;
62: }