生年月日から月齢を計算するアプリ
生年月日を入力してください:
以下が生年月日から月齢を計算するJavaScriptコードです。 ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>月齢計算アプリ</title> </head> <body> <h1>生年月日から月齢を計算するアプリ</h1> <p>生年月日を入力してください:</p> <input type="date" id="birthdate" /> <br /> <button onclick="calculateMoonAge()">計算する</button> <br /> <p id="moonage"></p> <script> function calculateMoonAge() { const birthdate = document.getElementById("birthdate").valueAsDate; const today = new Date(); const birthMonth = birthdate.getMonth() + 1; const birthYear = birthdate.getFullYear(); const birthDay = birthdate.getDate(); const currentMonth = today.getMonth() + 1; const currentYear = today.getFullYear(); let age = currentMonth - birthMonth; if (currentMonth < birthMonth || (currentMonth === birthMonth && today.getDate() < birthdate.getDate())) { age--; } if (age < 0) { age += 12; } const monthsOld = ((currentYear - birthYear) * 12) + age; const message = `あなたは${monthsOld}ヶ月前に生まれました。`; document.getElementById("moonage").textContent = message; } </script> </body> </html> ``` ジョークとしては、「月齢を計算するには、月に顔を向けて考える必要があります」というフレーズを追加することができます。