Die Aufgabe wurde anschliessend zum Taschenrechner in der Lehre zum Einstieg mit JavaScript gelösst.
Es sollte ein Programm umgesetzt werden welches von dem jetzigen Datum, zurück zu dem angegebenen Datum rechnet. Dann soll die bisher vergangene Zeit in Jahren, Tagen, Stunden, Minuten und Sekunden ausgerechnet und angegeben werden.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>birthday calculator</title>
<link href="style.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha256-4+XzXVhsDmqanXGHaHvgh1gMQKX40OUvDEBTu8JcmNs=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.0/moment.min.js" integrity="sha512-Izh34nqeeR7/nwthfeE0SI3c8uhFSnqxV0sI9TvTcXiFJkMd6fB644O64BRq2P/LA/+7eRvCw4GmLsXksyTHBg==" crossorigin="anonymous"></script>
</head>
<body>
<div class="all">
<input type="date" id="birthday"><br>
<input id="output">
<button class="startCovertion">load data</button>
<button class="clear">clear</button>
<input id="outputInYears">
<input id="outputInDays">
<input id="outputInHours">
<input id="outputInMinutes">
<input id="outputInSeconds">
<script src="script.js"></script>
</div>
<div class="record">
<p></p>
</div>
</body>
</html>
body {
font-family: arial;
background-color: #eef0f6;
}
#output {
width: 180px;
margin-top: 20px;
}
#outputInSeconds, #outputInMinutes, #outputInHours, #outputInDays, #outputInYears {
width: 100%;
border: 1px;
}
#outputInYears{
margin-top: 20px;
}
$('.startCovertion').on('click', (event)=> {
let calculatedDate = (new Date().getTime() - new Date($('#birthday').val()).getTime())/1000;
$('#outputInSeconds').val(calculatedDate + ' seconds');
getSingleData(calculatedDate)
getAllData(calculatedDate)
})
$(document).on('keydown', function (event) {
const pressedKey = event.key;
if (pressedKey === "Enter") {
let calculatedDate = (new Date().getTime() - new Date($('#birthday').val()).getTime())/1000;
$('#outputInSeconds').val(calculatedDate + ' seconds');
getSingleData(calculatedDate)
getAllData(calculatedDate)
}
})
$('.clear').on('click', (event)=> {
$('#birthday, #output, #outputInSeconds, #outputInMinutes, #outputInHours, #outputInDays, #outputInYears').val('')
})
function getAllData(calculatedDate) {
let calculatedYear = Math.floor(calculatedDate / 31536000);
calculatedDate = calculatedDate % 31536000;
let calculatedDay = Math.floor(calculatedDate / 86400);
calculatedDate = calculatedDate % 86400;
let calculatedHour = Math.floor((calculatedDate / 3600) + 2);
calculatedDate = calculatedDate % 3600;
let calculatedMinute = Math.floor(calculatedDate / 60);
calculatedDate = calculatedDate % 60;
let calculatedSecond = Math.floor(calculatedDate);
calculatedDate = calculatedDate - calculatedSecond;
$('#output').val(calculatedYear + ' y ' + calculatedDay + ' d ' + calculatedHour + ' h ' + calculatedMinute + ' min ' + calculatedSecond + ' s ')
}
function getSingleData(secondCalculation) {
secondCalculation = Math.floor(secondCalculation / 60);
$('#outputInMinutes').val(secondCalculation + ' minutes');
secondCalculation = Math.floor((secondCalculation / 60) + 2);
$('#outputInHours').val(secondCalculation + ' hours');
secondCalculation = Math.floor(secondCalculation / 24);
$('#outputInDays').val(secondCalculation + ' days');
secondCalculation = Math.floor(secondCalculation * 86400 / 31536000);
$('#outputInYears').val(secondCalculation + ' years');
}