Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Grabber 1

Status
Not open for further replies.

cabobound

Programmer
Jul 11, 2007
80
US
I am trying to get the remaining days of the year when a user exits a textbox. I have a date picker to get the date and found a script that I am modifying except its not going as planned.

I pass the date from the box to the fucntion but I always get a "d1.getYear() is not a function" error. I assume that once I get to the month and date I will have this same problem.

function days_between(d1) {
var date1 = new Date()
date1.setYear(d1.getYear())
date1.setMonth(d1)
date1.setDate(d1)
alert(date1);
...
}

I am not that proficient with js. Any ideas?


 
What are you passing to the function? Is d1 getting passed in as a date object? I would guess the answer is no because no other object type support the getYear method.

Make this change and tell me what gets alerted:
Code:
function days_between(d1) {
  [!]alert(d1);
  return false;[/!]
  var date1 = new Date()
  date1.setYear(d1.getYear())  
  date1.setMonth(d1)
  date1.setDate(d1)
  alert(date1);
  ...
}

-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]
 
I am using a date picker which gives me the mm/dd/yyyy. Is there a way to convert it?

ie. "d1=03/05/2008
 
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]
 
Its looking better. I did need the parseInt function. The one thing that is acting odd is the month. It setting it to April, even though the d1Array[0] is showing 3. Is this a base 0?
 
I just went through a number of months and set it to

date1.setMonth(parseInt(d1Array[0])-1);

and this works great.

Thank you!!!!!
 
Sorry, I forgot to mention that months are zero based in javascript (0 - 11). It looks like you've figured this out for yourself though [smile]

-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]
 
You'll want to use
Code:
parseInt(d1Array[0], [b][red]10[/red][/b])
because numbers that begin with zero are considered octal (base 8). That means that months 08 and 09 would cause an error without forcing a conversion to base 10.

Lee
 
Good catch Lee. I [!]ALWAYS[/!] specify the radix when using the parseInt function - better to take care of it in the first place than to have to debug it later.

-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]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top