0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

誕生日から、ある日付までの年齢計算 JavaScript

Posted at

■ユーザーインターフェイス

スクリーンショット (1121).png

■ソースコード

js main.js

    var birthYear = parseInt(document.getElementById('dead_birth_year').value);
    var birthMonth = parseInt(document.getElementById('dead_birth_month').value);
    var birthDay = parseInt(document.getElementById('dead_birth_day').value);

   
    var birthYearType = parseInt(document.getElementById('dead_birth_koyomi_type').value);
  
    if (birthYearType === -1) {
       
    } else if (birthYearType === 4) {
        birthYear += 2018;
    } else if (birthYearType === 3) { 
        birthYear += 1988;
    } else if (birthYearType === 2) {
        birthYear += 1925;
    } else if (birthYearType === 1) { 
        birthYear += 1911;
    } else if (birthYearType === 0) { 
        birthYear += 1868;
    }

    console.log("birthYear:::" + birthYear);

    
    var deadYear = parseInt(document.getElementById('dead_year').value);
    var deadMonth = parseInt(document.getElementById('dead_month').value);
    var deadDay = parseInt(document.getElementById('dead_day').value);

    
    var deadYearType = parseInt(document.getElementById('dead_koyomi_type').value);
    
    if (deadYearType === -1) { 
        
    } else if (deadYearType === 4) { 
        deadYear += 2018;
    } else if (deadYearType === 3) { 
        deadYear += 1988;
    } else if (deadYearType === 2) { 
        deadYear += 1925;
    } else if (deadYearType === 1) {
        deadYear += 1911;
    } else if (deadYearType === 0) {
        deadYear += 1868;
    }

    console.log("deadYear:::" + deadYear);
            
    
    var ageAtDeath = deadYear - birthYear;
    if ((deadMonth < birthMonth) || (deadMonth === birthMonth && deadDay < birthDay)) {
        ageAtDeath -= 1;
    }

    var resultTextBox = document.getElementById('Age_at_Death');

    if (!isNaN(ageAtDeath)) {
        var numericValue = Number(ageAtDeath);

        if (numericValue >= 0) {
            resultTextBox.value = numericValue;   
        }   
    } else {
   
    }
}
  
        document.getElementById('dead_birth_koyomi_type').addEventListener('change', calculateAge);
        document.getElementById('dead_birth_year').addEventListener('change', calculateAge);
        document.getElementById('dead_birth_month').addEventListener('change', calculateAge);
        document.getElementById('dead_birth_day').addEventListener('change', calculateAge);

        document.getElementById('dead_koyomi_type').addEventListener('change', calculateAge);
        document.getElementById('dead_year').addEventListener('change', calculateAge);
        document.getElementById('dead_month').addEventListener('change', calculateAge);
        document.getElementById('dead_day').addEventListener('change', calculateAge);

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?