1

Javascript rookie here:

function calcExpiration(){
    var getState = jQuery( "#state" ).text();
    var getcalldate = jQuery( "#date" ).text();
    var date = new Date( getcalldate  );
    if (getState == "KY" || getState == "IN"){
        date.setDate(date.getDate() + 30);
    }
    if (getState == "WV" || getState == "OH"){
        date.setDate(date.getDate() + 10);
    }
    var expmonth = date.getMonth()+1;
    var expdate = date.getDay();
    var expyear = date.getFullYear();
    jQuery( "#expiration" ).html( expmonth + "/" + expdate + "/" + expyear);
}
calcExpiration();

When getcalldate = 01/02/2015

Returns 2/0/2015

What am I missing?

abl
  • 5,970
  • 4
  • 25
  • 44
dgodfather
  • 41
  • 1
  • 7
  • You shouldn't use the Date constructor to parse strings, e.g. in `var date = new Date( getcalldate )`. It's inconsistent and unreliable across browsers. Much better tell users the format you require and parse it yourself (see [*javascript Date.parse*](http://stackoverflow.com/questions/2587345/javascript-date-parse/2587398#2587398)). – RobG Jan 03 '15 at 21:20

1 Answers1

2

If i understand your question correctly , you should call getDate() to return the day of the month, and not getDay() which will return 0 because your date (February 1st 2015) is Sunday and the function returns Sunday = 0, Monday = 1 etc.

Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89
Fevly Pallar
  • 3,059
  • 2
  • 15
  • 19