Hi all. I’m trying to build a calculator that works out when certain documents will expire based on the issue date of the document provided by the user. Currently in my code it is set to do this by adding 90 days to the issue date.
A second thing that also needs to be calculated is when users should reapply for the document. So if a document expires on the 3rd of May 2022 they should reapply 10 days before the expiry date.
The problem that I don’t know how to fix is that the subtraction is taking place from the current date instead of the from the expiry date.
Any advice would be greatly appreciated.
Please find the full code below and in the codepen: https://codepen.io/Nicole850206/pen/bGjRXJa
<!DOCTYPE html>
<html>
<body>
<p>Select a date: <input type="date" id="issueDate"></p>
<button onclick="addDays()">Add 90 days</button>
<button onclick="subtractDays()">Subtract 10 days</button>
<div id="expiryDate"></div>
<div id="applyDate"></div>
<script>
function addDays() {
var date = new Date(document.getElementById("issueDate").value);
date.setDate(date.getDate() + 90);
document.getElementById("expiryDate").innerHTML = date;
}
function subtractDays() {
var expiryDate = document.getElementById("expiryDate").value;
var date = expiryDate ? new Date(expiryDate) : new Date();
date.setDate(date.getDate() - 10);
document.getElementById("applyDate").innerHTML = date;
}
</script>
</body>
</html>