JavaScript Market - 動きのあるホームページを作る為の各種JavaScript のサンプルと小技集を公開中

JavaScript

JavaScriptって…? | JavaScript応用のツボ | ブラウザについて | JavaScript Market 利用規程 | JavaScript 各種サンプル | JavaScript 小技集

日付から曜日を調査する

日付を入力してボタンをクリックすると何日前(何日後)の何曜日かを表示します。

■ このスクリプトのソース
<script>
/* 選択ボックスの西暦欄の選択肢の数(現在の西暦±この値年分) */
yl = 100;

wday = new Array('日','月','火','水','木','金','土');

function rst() {
   nd = new Date();
   ng = nd.getTime();
   fy = nd.getYear();
   fm = nd.getMonth();
   fd = nd.getDate();
}
rst();

function wday_search() {
   tdate = new Date(fy,fm,fd);
   ty = tdate.getYear();
   if (fy >= 1900 && fy <= 1999) ty += 1900;
   tm = tdate.getMonth();
   td = tdate.getDate();
   tw = tdate.getDay();
   tg = tdate.getTime();
   ke = tg - ng;
   ke = Math.ceil(ke / 1000 / (24 * 60 * 60));
   if (fm != tm || fd != td)
      alert("入力された日付は存在しません\n\n" +
      ty + "年" + (eval(tm)+1) + "月" + td + "日に換算して結果を出力します");
   if (ke == 0) mes = "本日";
   else if (ke < 0) mes = (ke * -1) + "日前の";
   else mes = ke + "日後の";
   document.forms[0].out.value = mes + wday[tw] + "曜日です";
}
document.write("<form>");
document.write("<select onChange='fy=this.options[this.selectedIndex].value'>");
for(i = (fy-yl); i <= (fy+yl); i++) {
   if (i == fy) document.write("<option value=",i," selected>",i);
   else document.write("<option value=",i,">",i);
}
document.write("</select>年 ");
document.write("<select onChange='fm=this.options[this.selectedIndex].value'>");
for(i = 0; i < 12; i++) {
   if (i == fm) document.write("<option value=",i," selected>",i+1);
   else document.write("<option value=",i,">",i+1);
}
document.write("</select>月 ");
document.write("<select onChange='fd=this.options[this.selectedIndex].value'>");
for(i = 1; i <= 31; i++) {
   if (i == fd) document.write("<option value=",i," selected>",i);
   else document.write("<option value=",i,">",i);
}
document.write("</select>日 ");
document.write("<input type=button onclick='wday_search()' value='は何曜日?'>");
document.write("<p><input size=30 name=out> <input type=reset onclick='rst()'></form>");
</script>


suepon.com | CGI作成・CGI制作 | ドメイン検索 | バナー自動作成 | Google Dance jp | 松浦亜弥 (H.P.memo) | パソコン | ネイル