I look after the website for my not-for-profit swimming club; it's hosted by Squarespace and our Treasurer has asked me to add a calculator to the membership page so prospective members can easily calculate the pro-rata amount required if they wish to pay in one lump sum. A full year if paid annually costs £475 so if the button was clicked on June 30th I'd expect it to return £237.50 on the page. Looking at some other answers and using my limited Javascript knowledge I've come up with this, but it's not giving me the answer I would expect:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display the total pro-rata annual amount due, if membership starts tomorrow</p>
<button onclick="myFunction()">Click Me</button>
<p id="prorata"></p>
<script>
function myFunction() {
var today = new Date();
var totalDays = new Date(today.getFullYear(), today.getMonth(), 0).getDate();
var daysLeft = (totalDays - today.getDate()) - 1;
var pricePerDay = 1.30;
var n = daysLeft * pricePerDay;
document.getElementById("prorata").innerHTML = n;
}
</script>
</body>
</html>
Any help is much appeciated!