Sounds like your function is getting passed a string in a common date format. Since String objects do not support the getYear method, this explains why you are getting an error.
Now, let's put all that aside and assume that you're not getting an error. In the code you first posted, what do you expect these 2 lines to do:
Code:
function days_between(d1) {
var date1 = new Date()
date1.setYear(d1.getYear())
[!] date1.setMonth(d1)
date1.setDate(d1)[/!]
alert(date1);
...
}
From your last post you said a valid value for the d1 variable could be "03/05/2008". This means in the 2 highlighted lines of code above, you are setting the month to "03/05/2008", and then setting the date to "03/05/2008". It is not difficult to see that this will not produce the results you are expecting.
There are a bunch of different ways to go about this. The most obvious way to do this is to use the
substr method to extract the month, date, and year from the string. However, I would personally use the
split method to break the string into an array on the "/" character. Something like this:
Code:
function days_between(d1) {
var d1Array = [!]d1.split(/\//)[/!];
var date1 = new Date();
date1.setYear([!]d1Array[2][/!]);
date1.setMonth([!]d1Array[0][/!]);
date1.setDate([!]d1Array[1][/!]);
alert(date1);
...
}
Note that this will break the d1 string into 3 seperate strings - but the setYear, setMonth, and setDate arrays expect an integer parameter - so you may need to convert them to integers before passing them to the methods. This is done by using the
parseInt method
A side note - remember to always put a semicolon at the end of each line of code. It will work w/o it, but it is bad programming practice to leave them out.
-kaht
Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P>
<.</B>[/small]