Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javascript calculating between 2 dates

 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey all,

I want to calculate between 2 dates to display an age...

the date of birth is 4 Feb 1981, while taking that date away from today.

I tried this, and so far it kind of works.


as I said, this works for the most part, but when I change my computer clock to test this, if I am in February at all it does not update the age.

eg. I changed my comp clock to 3 Feb 2008, the age should be 26, but its 27, why is this?

what am I doing wrong?

davy
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basic debugging: what are the values of the variables when you don't get the expected answer?
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no matter what date in february in any year it does not move on to expected values.

so right now it should be 26, if I change comp clock to 3 feb 07, the age should be 25, it stays 26, no matter what date in feb

davy
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is simple math. When the result of the subtraction is not as expected, what are the unexpected values going into the subtraction operation?

Instead of saying, "I'm getting the wrong result", find out why. What wrong values are being fed into the expression? It's pretty clear that you haven't found a bug in the subtraction operator, os the problem must be in the operands. What are they? How are they different from what you expect?
[ August 01, 2007: Message edited by: Bear Bibeault ]
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you talking about putting in getYear instead of getFullYear?

davy
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what Bear's trying to say is:
"alert()" is your friend.

if z in (x - y = z) is not what you expect wouldn't you first want to find out exactly what x and y are?
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nope still not with you

bear in mind, my brain has been on this all day.

also I am trying everything out all the time

davy
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I seen what I was kind of doing wrong,

I fixed a bit of it, I just cant get the year,



davy
[ August 01, 2007: Message edited by: Davy Kelly ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, not to put words into Bear's mouth but, I think you're still missing the point about debugging.

If this were my issue, the first thing I would want to do is narrow down the two variables that are going to be used in the operation and verify what they are.

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If birth or today are not what you're expecting, then you've narrowed the problem down to one issue which you can work on exclusively until it's fixed.
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well the birthday is set and that never changes.....

todays date always changes, but is dependent of the systems time....

I thought if I get the difference of both the millisecond and then get the full year out of that? problems is leap years....

davy
p.s. so its a case fo finding the difference between the days and months...

[ August 01, 2007: Message edited by: Davy Kelly ]
[ August 01, 2007: Message edited by: Davy Kelly ]
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have even tested this, and it does not work, it does not update properly.



davy

EDIT: I think I got this code to work well, so far:

EDIT: well tested again, and it still does not seem to update well. sometimes it does sometimes it does not, not sure what is going on.

Bearing in mind I am changing my computer clock to test this code out, and I refresh the page all the time.

[ August 02, 2007: Message edited by: Davy Kelly ]
[ August 02, 2007: Message edited by: Davy Kelly ]
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I finally got it.

My problem was not looking for both at the same time.

so here is my new improved code, whiter than white... hehe

hope its right, I tested by changing the comp clock from Feb 3 and Feb 4, it seems to work well.



davy
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, if compatibility with older browsers is a concern you should not use getFullYear() but getYear() instead.

Here is an explanation of why and how:
http://www.quirksmode.org/js/introdate.html#year
[ August 02, 2007: Message edited by: Valentin Crettaz ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic