0

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
3
  • 3
    Not suprisingly, "2014-08-19" - "2014-08-31" is not a number ? Commented Aug 22, 2014 at 10:21
  • are IDs 'fromdate' 'todate' or 'from_date' 'to_date' Commented Aug 22, 2014 at 10:22
  • It would be useful to know if you're using a datepicker or just typing the dates. If you're using something like jQuery's datepicker it would give you totally different answers. Commented Aug 22, 2014 at 10:31

4 Answers 4

2

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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);

DEMO

4 Comments

Close, but no cigar, the OP is getting the values as strings from inputs.
@adeneo, how about now?
That is closer, and the OP could insert the strings from the values, so I'll give it a +1
@user3843997, you are wel come to accept the answer. plz do it :) ;)
0

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;

Comments

-1

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

4 Comments

That's like writing parseInt("2014-08-19") - parseInt("2014-08-31"), I'm not sure you'd get the expected result ?
Yep parseInt("2014-08-19" ) yields just the first int it finds
I dont think that they are in date format. OP would have not done the operation of multiplying with integer values.
You sould use new Date().getTime(string); to get the date parsed in ms

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.