ScottC
ScottC

Reputation: 462

Javascript subtracting fine, adding fails

I have a webpage made of a lot of PHP and Jquery/javascript

I have a function that runs when a submit button is clicked

$("#yesterday").click(function(){
            var $date = $.datepicker.parseDate('yymmdd','<?php echo $_POST['date']; ?>');
            var julDate = $.datepicker.formatDate('yyoo',new Date($date));
            Number(julDate);
            julDate = julDate - 1;
            String(julDate);
            var $date = $.datepicker.parseDate('yyoo',julDate);
            var julDate = $.datepicker.formatDate('yymmdd',new Date($date));
            var $longDate = $.datepicker.formatDate('DD MM d, yy',new Date($date));
            $("#date").val(julDate);
            $("#longdate").val($longDate);
            $("#orwell_btn").click();
return false;
});

This function works PERFECTLY it reduces the date by 1 and reloads the page and executes my SQL query returnning all the correct info

if I change the fifth line and make the function liek so:

    $("#tomorrow").click(function(){
            var $date = $.datepicker.parseDate('yymmdd','<?php echo $_POST['date']; ?>');
            var julDate = $.datepicker.formatDate('yyoo',new Date($date));
            Number(julDate);
            julDate = julDate + 1;
            String(julDate);
            var $date = $.datepicker.parseDate('yyoo',julDate);
            var julDate = $.datepicker.formatDate('yymmdd',new Date($date));
            var $longDate = $.datepicker.formatDate('DD MM d, yy',new Date($date));
            $("#date").val(julDate);
            $("#longdate").val($longDate);
            $("#orwell_btn").click();
return false;
});

the function doesn't complete and my page loads but as if it would on the very first load (which I have logic to handle but it should be advancing by 1 day ie doing the opposite of the yesterday button)

seems very odd to me

Upvotes: 0

Views: 323

Answers (5)

Emad Samir Zaki
Emad Samir Zaki

Reputation: 413

change to :

julDate = Number(julDate);
julDate =julDate +1;

Upvotes: 0

Ortiga
Ortiga

Reputation: 8814

That's because

string - int -> tries to parse string as int, then subtract

string + int -> Convert int to string, then appends. So, if you have "3" as month, you end up with "31"

Upvotes: 1

m90
m90

Reputation: 11822

This is a simple typing problem. Your julDate is still a String when you add/subtract the 1 (as Number( will return a value - that you never assign - but not convert the passed variable). When you subtract the type is converted to a number as a - can only be performed on a numeric value. Yet, + can also be used to concatenate strings and will therefore treat your variable like a String.

See this fiddle for clarification: http://jsfiddle.net/wRbwX/1/

All you need to do is convert your string to a number (by doing parseInt for example) and it should be working

Upvotes: 2

JayC
JayC

Reputation: 7141

Number(julDate); doesn't do anything to julDate AFAIK. Perhaps you mean julDate = Number(julDate);?

Upvotes: 4

SLaks
SLaks

Reputation: 887657

Number(julDate) returns a number with the parsed value.
It doesn't actually change anything.

Therefore, julDate + 1 is string concatenation.

You want julDate = Number(julDate).

Upvotes: 6

Related Questions