[英]Javascript adding/removing values
I have this JS Code: 我有这个JS代码:
$("#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. 我在说NaN时遇到错误。
i am entering the following: 我输入以下内容:
#from_date = 2014-08-19
#to_date = 2014-08-31
#cost_per_month = 2.60
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? 研究如何在JavaScript中获取两个日期之间的天数? This could be helpful! 这可能会有所帮助!
You have not parsed textbox values to int: 您尚未将文本框值解析为int:
var days_between = parseInt($("#todate").val()) - parseInt($("#fromdate").val())
and 和
var year_cost = parseInt($("#cost_per_month").val()) * 12
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);
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.