Reputation: 205
I have this JS Code:
$("#submit").on('click',function() {
//work out number of days between the two dates
var days_between = $("#todate").val() - $("#fromdate").val()
//do the cost per month times 12 (months)
var year_cost = $("#cost_per_month").val() * 12
//do the yearly cost / 365
var daily_cost = year_cost / 365
var daily_cost = parseFloat( daily_cost.toFixed(2) )
//now do the daily cost times cost_per_month
var total_cost = daily_cost * days_between
$(".total_days").html(days_between);
$(".total_cost").html(total_cost);
})
I am getting an error saying NaN though.
i am entering the following:
#from_date = 2014-08-19
#to_date = 2014-08-31
#cost_per_month = 2.60
Upvotes: 0
Views: 75
Reputation: 2584
The way you are calculating number of dayes between the dats is wrong. look into this How do I get the number of days between two dates in JavaScript? This could be helpful!
Upvotes: 2
Reputation: 5962
you can't get total days directly
var tDate = new Date($("#todate").val());
var fDate = new Date($("#fromdate").val());
var diff=tDate-fDate;
This would give you the difference in milliseconds between the two dates.
var DaysNo= diff / 1000 / 60 / 60 / 24;
Upvotes: 0
Reputation: 4906
This Will Work
//work out number of days between the two dates
var oneDay = 24 * 60 * 60 * 1000;
//var date1 = new Date("2014,08,31");
//var date2 = new Date("2014,08,19");
var date1 = new Date("2014-08-31");
var date2 = new Date("2014-08-19");
var Daysd = date1.getDate() - date2.getDate();
//var days_between = date1 - date2
var diffDays = Math.round(Math.abs((date1.getTime() - date2.getTime()) / (oneDay)));
//do the cost per month times 12 (months)
var year_cost = parseInt(2.60) * 12
alert(Daysd);
alert(year_cost);
Upvotes: 1
Reputation: 82231
You have not parsed textbox values to int:
var days_between = parseInt($("#todate").val()) - parseInt($("#fromdate").val())
and
var year_cost = parseInt($("#cost_per_month").val()) * 12
Upvotes: -1