//============================================================================= // // GkSeventeen.js JAVAScript 17歳+〇日計算 // Programmed by G.K.2018 // //=============================================================================
//変数の宣言=================================================================== var canvas; //キャンバスオブジェクト var ctx; //描画コンテキスト
var winWidth = 600; //CANVASの横幅 var winHeight = 400; //CANVASの高さ
var now_year; // 現在の年月日 数値 var now_mon; var now_days;
var now_date; // 2018/06/30形式の現在の日付 var base_date; // 〃 17歳誕生日の日付
//関数の宣言========================================================================
//初期化:年選択のオプション追加------------------------------- function initSelect_Year() {
// 現在の年を取得 var now = new Date(); now_year = now.getYear() + 1900; now_mon = now.getMonth(); now_days = now.getDate();
// 年入力ボックスのメニュー項目のセット----- var selectId = document.form1.bdYear;
for (var i = now_year-100; i < now_year+1; i++) { element = document.createElement('option'); element.setAttribute('value', i); element.innerHTML = i; selectId.appendChild( element ); }
// 現在の年を選択状態にする selectId.options[100].selected = true;
// 月入力ボックスのメニュー項目のセット----- selectId = document.form1.bdMonth;
for (var i = 1; i < 12; i++) { element = document.createElement('option'); element.setAttribute('value', i); element.innerHTML = i; selectId.appendChild( element ); }
// 現在の月を選択状態にする selectId.options[now_mon].selected = true;
// 日入力ボックスのメニュー項目のセット----- selectId = document.form1.bdDay;
// メニュー項目のセット for (var i = 1; i < 32; i++) { element = document.createElement('option'); element.setAttribute('value', i); element.innerHTML = i; selectId.appendChild( element ); }
// 現在の日を選択状態にする selectId.options[now_days].selected = true;
}
//初期化処理------------------------------------------- function init() {
//描画コンテキストの取得 canvas = document.getElementById("mainCanvas"); if ( ! canvas || ! canvas.getContext ) { return false; } ctx = canvas.getContext('2d');
initSelect_Year(); //年月日選択オプションの初期化
ctx.clearRect(0, 0, 600, 400); //画面のクリア }
//計算実行-------------------------------------------- function execCulc() {
ctx.clearRect(0, 0, 600, 400); //画面のクリア ctx.font = "22px 'MS Pゴシック'"; //表示フォントの設定
//誕生日の文字列-------------------------------- var selectId = document.form1.bdYear; var num = selectId.selectedIndex; var str_year = selectId.options[num].value;
selectId = document.form1.bdMonth; num = selectId.selectedIndex; var str_mon = selectId.options[num].value;
selectId = document.form1.bdDay; num = selectId.selectedIndex; var str_days = selectId.options[num].value;
var str_birthday = str_year + "年" + str_mon + "月" + str_days + "日"; ctx.fillText("あなたの誕生日:", 30, 50); ctx.fillText(str_birthday, 230, 50);
//現在の日付の文字列--------------------------- var str_nowdate = now_year + "年" + (now_mon + 1) + "月" + (now_days + 1) + "日"; ctx.fillText("今日の日付:", 30, 100); ctx.fillText(str_nowdate, 230, 100);
//現在の年月日を作成 var now_mon2 = Number(now_mon) + 1; var now_days2 = Number(now_days) + 1; now_date = now_year + "/" + now_mon2 + "/" + now_days2;
ctx.fillText("↓↓↓", 50, 150);
//17歳を超えているか判定---------------------- var tmp_year = Number(str_year) + 17; var result1 = 0; if(tmp_year < now_year) { result1 = 1; //年が達していない } else { if(tmp_year == now_year) { //年は達している if(str_mon < now_mon + 1) { //月が達していない result1 = 1; } else { if(str_mon == now_mon + 1) { //月は達している if(str_days <= now_days + 1) { //日が達していない result1 = 1; } } } } }
//計算結果表示 if(result1 == 0) { ctx.fillText("まだ17歳になっていない。", 30, 200); } else {
//17歳の誕生日の年月日を作成 var str_mon2 = Number(str_mon); var str_days2 = Number(str_days); base_date = tmp_year + "/" + str_mon2 + "/" + str_days2;
var diffDays = culcDiffDays(); //日付差分を計算
ctx.fillStyle = "blue"; ctx.fillText("あなたの年齢は:", 30, 200); ctx.fillText("17歳+" + diffDays + "日", 230, 200); ctx.fillStyle = "black"; }
}
//日付の差分を計算------------------------------------ function culcDiffDays() { var diffDays = 0;
var Date1 = new Date(now_date); var Date2 = new Date(base_date);
diffDays = Math.ceil((Date1 - Date2) / 86400000); //1日は86400000ms
return diffDays; }
//最初に実行------------------------------------------ function main(){ init(); //draw(); }
-
|