• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

javascript calculating between 2 dates

 
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are you talking about putting in getYear instead of getFullYear?

davy
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic