اريد كود جافا لحساب العمر بالسنة والشهر
سأضيف لك كودين جافا استخدام الذي يناسبك مع انه يمكنك تعديل اي كود جافا لحساب العمر بما يتناسب مع صفحتك
لتضمين كود Java في صفحة HTML ، يمكنك استخدام
فيما يلي مثال لكيفية استخدام هذا العنصر لحساب عمر شخص بناءً على سنة ميلاده:
Copy code
<html>
<head>
<title>Calculate Age</title>
</head>
<body>
<h1>Calculate Age</h1>
<form>
Enter your birth year:
<input type="text" id="birthYear">
<input type="button" value="Calculate" onclick="calculateAge()">
</form>
<p>Your age is: <span id="age"></span></p>
<script>
function calculateAge() {
// Get the user's birth year from the form
var birthYear = document.getElementById("birthYear").value;
// Get the current year
var currentYear = new Date().getFullYear();
// Calculate the age
var age = currentYear - birthYear;
// Display the age
document.getElementById("age").innerHTML = age;
}
</script>
</body>
</html>
كود جافا الاخر لحساب العمر
Copy code
function calculateAge() {
// Get the user's birth year, month, and day from the form
var birthYear = document.getElementById("birthYear").value;
var birthMonth = document.getElementById("birthMonth").value;
var birthDay = document.getElementById("birthDay").value;
// Create a date object for the user's birthday
var birthDate = new Date(birthYear, birthMonth - 1, birthDay);
// Get the current date
var currentDate = new Date();
// Calculate the age in years
var ageInYears = currentDate.getFullYear() - birthDate.getFullYear();
// Subtract one from the age if the current month and day are before the user's birthday
if (currentDate.getMonth() < birthDate.getMonth() || (currentDate.getMonth() == birthDate.getMonth() && currentDate.getDate() < birthDate.getDate())) {
ageInYears--;
}
// Calculate the age in months
var ageInMonths = (ageInYears * 12) + (currentDate.getMonth() - birthDate.getMonth());
// Subtract one from the age in months if the current day is before the user's birthday
if (currentDate.getDate() < birthDate.getDate()) {
ageInMonths--;
}
// Calculate the age in days
var ageInDays = Math.floor((currentDate - birthDate) / (1000 * 60 * 60 * 24));
// Display the age in years, months, and days
document.getElementById("age").innerHTML = ageInYears + " years, " + ageInMonths + " months, and " + ageInDays + " days";
}
هذا كود مطّور لو تحب تستخدمه
Copy code
<html>
<head>
<title>Calculate Age</title>
</head>
<body>
<h1>Calculate Age</h1>
<form>
Enter your birth year:
<input type="text" id="birthYear">
Enter your birth month:
<select id="birthMonth">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
Enter your birth day:
<input type="text" id="birthDay">
<input type="button" value="Calculate" onclick="calculateAge()">
</form>
<p>Your age is: <span id="age"></span></p>
<script>
function calculateAge() {
// Get the user's birth year, month, and day from the form
var birthYear = document.getElementById("birthYear").value;
var birthMonth = document.getElementById("birthMonth").value;
var birthDay = document.getElementById("birthDay").value;
// Create a date object for the user's birthday
var birthDate = new Date(birthYear, birthMonth - 1, birthDay);
// Get the current date
var currentDate = new Date();
// Calculate the age in years
var ageInYears = currentDate.getFullYear() - birthDate.getFullYear();
// Subtract one from the age if the current month and day are before the user's birthday
if (currentDate.getMonth() < birthDate.getMonth() || (currentDate.getMonth() == birthDate.getMonth() && currentDate.getDate() < birthDate.getDate())) {
ageInYears--;
}
// Calculate the age in months
var ageInMonths = (ageInYears * 12) + (currentDate.getMonth() - birthDate.getMonth());
// Subtract one from the age in months if the current day is before the user's birthday
if (currentDate.getDate() < birthDate.getDate()) {
ageInMonths--;
}
// Display the age in years and months
document.getElementById("age").innerHTML = ageInYears + " years and " + ageInMonths + " months";
}
</script>
</body>
</html>