/*----------------------------------------
* daysuntil.js
* Copyright (C) 2020 StraightApps.com, All Rights Reserved
* September 14, 2020 初期版
*-----------------------------------------*/
/*----------------------------------------
* ここに直接書いたコードは、読み込み時に実行される。
*-----------------------------------------*/
var elem;
var str, strTmp;
// 本日の日付を表示します。
elem = document.getElementById("today");
str = "今日は " + date.getFullYear() + " 年 " + (date.getMonth()+1) + " 月 " + date.getDate() + " 日です。";
elem.innerHTML = str;
// 年コンボボックスを作成します。
setYearCombo();
// 月コンボボックスを作成します。
setMonthCombo();
// 日コンボボックスを作成します。
setDayCombo(true);
// 初期状態の「あと何日」
elem = document.getElementById("days");
elem.innerHTML = 'あと 0 日です。';
/*----------------------------------------
* 「年」コンボボックスの作成
*-----------------------------------------*/
function setYearCombo()
{
elem = document.getElementById("year");
str = '';
elem.innerHTML = str;
// 選択中の年を記録
selYear = year;
}
/*----------------------------------------
* 「月」コンボボックスの作成
*-----------------------------------------*/
function setMonthCombo()
{
elem = document.getElementById("month");
str = '';
elem.innerHTML = str;
// 選択中の月を記録
selMonth = month;
}
/*----------------------------------------
* 「日」コンボボックスの作成
*-----------------------------------------*/
function setDayCombo(bToday)
{
elem = document.getElementById("day");
str = '';
elem.innerHTML = str;
// 選択中の日を記録
selDay = day;
}
/*----------------------------------------
* 「年」コンボボックスの値が変更されたとき
* リストを表示しても変更なければ呼び出されません。
*-----------------------------------------*/
function changeYear(selectedYear)
{
// うるう年に当たる場合は、2月の日数が変わります。
selYear = selectedYear;
// 2月選択中以外は、何もする必要はありません。
if (selMonth != 2){
return;
}
// うるう年かもしれないので、日コンボを再設定します。
// 2月に29日がある、or 2月29日選択中だが無効
setDayCombo(false);
}
/*----------------------------------------
* 「月」コンボボックスの値が変更されたとき
*-----------------------------------------*/
function changeMonth(selectedMonth)
{
selMonth = selectedMonth;
// 日コンボを再設定します。
setDayCombo(false);
}
/*----------------------------------------
* 「日」コンボボックスの値が変更されたとき
*-----------------------------------------*/
function changeDay(selectedDay)
{
selDay = selectedDay;
}
/*----------------------------------------
* 「計算する」ボタン処理
*-----------------------------------------*/
function calc()
{
elem = document.getElementById("days");
nDiff = getDaysUntil();
elem.innerHTML = 'あと ' + nDiff + ' 日です。';
}
function getDaysUntil()
{
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var eom;
var nDiff = 0; // あと何日か
//----------------------------------------
// 年月が同じとき、日の差だけで計算できます。
if (year == selYear && month == selMonth){
nDiff = parseInt(selDay) - parseInt(day);
return nDiff;
}
//----------------------------------------
// 年が同じで月だけ違うとき(年と月が同じケースは処理済み)
if (year == selYear){
// 今月の残り日数を計算します。
// 31日ある月の 30日なら、今月の残り日数は 1 です。
eom = new Date( year, month, 0 ).getDate(); // 選択中年月の End of Month
nDiff = parseInt(eom) - parseInt(day);
// 挟まっている月の日数を加算します。
var curMonth = month + 1; // 翌月
while (curMonth < selMonth){ // 目標の月の前月まで
eom = new Date( year, curMonth, 0 ).getDate(); // 指定年月の End of Month(もとの値は自動破棄)
nDiff += parseInt(eom);
curMonth ++;
}
// 目標の月の日数を加算します。
nDiff += parseInt(selDay);
return nDiff;
}
//----------------------------------------
// 年も違うとき
// 今年の12月31日までの日数を計算
nDiff = calcDaysWithinTheYear();
// 挟まっている年の日数を加算します。
var curYear = year + 1; // 翌年
while (curYear < selYear){ // 目標の年の前年まで
nDiff += (isLeapYear(curYear) ? 366 : 365);
curYear ++;
}
// 挟まっている月の日数を加算します。
var curMonth = 1; // 1月から
while (curMonth < selMonth){ // 目標の月の前月まで
eom = new Date( selYear, curMonth, 0 ).getDate(); // 指定年月の End of Month
nDiff += parseInt(eom);
curMonth ++;
}
// 目標の月の日数を加算します。
nDiff += parseInt(selDay);
return nDiff;
}
// 同年内の残り日数を計算します(12月31日までの日数)。
function calcDaysWithinTheYear()
{
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var eom;
var nMonthTo = 12;
var nDayTo = 31;
// 今月の残り日数を計算します。
// 31日ある月の 30日なら、今月の残り日数は 1 です。
var eom = new Date( year, month, 0 ).getDate();
var nDiff = parseInt(eom) - parseInt(day);
// 今月が12月の場合は、nDayTo をクリアしておく必要があります。
if (month == 12){
nDayTo = 0;
}
// 挟まっている月の日数を加算します。
var curMonth = month+ 1; // 翌月
while (curMonth < nMonthTo){ // 目標の月の前月まで
eom = new Date( year, curMonth, 0 ).getDate(); // 指定年月の End of Month(もとの値は自動破棄)
nDiff += parseInt(eom);
curMonth ++;
}
// 12月の日数を加算します。
nDiff += parseInt(nDayTo);
return nDiff;
}
/**
* うるう年かどうかを返します。
* 4で割り切れる年は、うるう年です。
* しかし、100でも割り切れる年は、うるう年ではありません。
* しかし、400でも割り切れる年は、うるう年です。
*/
function isLeapYear(year)
{
// 4で割り切れない年は、うるう年ではありません。
if (Math.floor(year / 4) != (year / 4)){
return false;
}
// 4で割り切れて、400でも割り切れるなら、うるう年です。
if (Math.floor(year / 400) == (year / 400)){
return true;
}
// 4で割り切れ、400では割り切れないが、100で割り切れるなら、うるう年ではありません。
if (Math.floor(year / 100) == (year / 100)){
return false;
}
// 4で割り切れ、100でも 400でも割れない年は、うるう年です。
return true;
}