I'm very new to Javascript, and can't figure out the correct formulas to determine how many days have been used vs not used.
I am trying to create a basic form calculator. It has 2 fields, Billing Cycle Date, and Plan Change Date. It should calculate the difference between the two numbers.
The issue is sometimes the Billing Cycle Date is lower than the Plan Change date which results in a negative number which throws off my current formula.
What am I doing wrong?
I have tried different formulas and even tried displaying only positives.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form>
<input placeholder="Billing Cycle Date" type="text" onChange="calc()" id="BillingCycleDate"/>
<input placeholder="Plan Change Date" type="text" onChange="calc()" id="PlanChangeDate"/>
<input type="button" onClick="calc()" value="Calculate" />
</form>
<div id="BillingCycleDate_Result"></div>
<div id="PlanChangeDate_Result"></div>
<div id="DaysUsed_Result"></div>
<div id="DaysUnused_Result"></div>
<div id="DaysUsedDaysUnused_Result"></div>
<script>
function calc(){
//Imports data from form - Billing Cycle Date
var BillingCycleDateJS=document.getElementById('BillingCycleDate').value;
//Imports data from form - Plan Change Date
var PlanChangeDateJS=document.getElementById('PlanChangeDate').value;
//Calculates - Used Days
var DaysUsed=+BillingCycleDateJS-PlanChangeDateJS;
//Calculates - Unused Days
var DaysUnused=+PlanChangeDateJS-BillingCycleDateJS;
//Calculates - Days Used & Unused (Should always = 30)
var DaysUsedDaysUnused=DaysUsed+DaysUnused;
//Prints Result - Data Billing Cycle Date
document.getElementById('BillingCycleDate_Result').innerHTML='<br>Billing Cycle Date: ' + BillingCycleDateJS + '<br><br>';
//Prints Result - Plan Change Date
document.getElementById('PlanChangeDate_Result').innerHTML='Plan Change Date: ' + PlanChangeDateJS + '<br><br>';
//Prints Result - Days Used
document.getElementById('DaysUsed_Result').innerHTML='Days Used: ' + DaysUsed + '<br><i>(If billing cycle = 1 & plan change date = 10, answer should be: 9)<br>(If billing cycle = 10 & plan change date = 1, answer should be: 21)</i><br><br>';
//Prints Result - Data Unused
document.getElementById('DaysUnused_Result').innerHTML='Days Unused: ' + DaysUnused + '<br><i>(If billing cycle = 1 & plan change date = 10, answer should be: 21)<br>(If billing cycle = 10 & plan change date = 1, answer should be: 9)</i><br><br>';
//Prints Result - Billing Cycle Date + Plan Change Date
document.getElementById('DaysUsedDaysUnused_Result').innerHTML='Days Used+Unused: ' + DaysUsedDaysUnused + '<br><i> (Billing Cycle+Plan Change, Should always = 30)</i>';
return false
}
</script>
</body>
</html>
If billing date = 1
If plan change date = 10
Actual Results:
Days Used: -9
Days Unused: 9
Expected Results:
Days Used: 9
Days Unused: 21
If billing date = 10
If plan change date = 1
Actual Results:
Days Used: 9
Days Unused: -9
Expected Results:
Days Used: 21
Days Unused: 9