Search code examples
javascripthtmlsquarespace

Javascript to display pro-rata membership amount


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!


Solution

  • is this you wanted ?? I did like this : remaining_Days = (this year last day - today);

    Pro rata = remaining_Days * 1.30;

    <!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 year = new Date().getFullYear();
      
    var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
    var firstDate = new Date();
    
    var secondDate = new Date(year,11,31);
    
    var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
    
        var n = diffDays* 1.30;
        document.getElementById("prorata").innerHTML = n;
    }
    </script>
    
    </body>
    </html>