This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes javascript calculating between 2 dates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "javascript calculating between 2 dates" Watch "javascript calculating between 2 dates" New topic
Author

javascript calculating between 2 dates

Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
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


How simple does it have to be???
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Basic debugging: what are the values of the variables when you don't get the expected answer?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
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

Joined: Jan 10, 2002
Posts: 60752
    
  65

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

Joined: Jan 12, 2004
Posts: 384
are you talking about putting in getYear instead of getFullYear?

davy
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
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

Joined: Jan 12, 2004
Posts: 384
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jan 12, 2004
Posts: 384
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

Joined: Jan 12, 2004
Posts: 384
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

Joined: Jan 12, 2004
Posts: 384
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

Joined: Aug 26, 2001
Posts: 7610
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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javascript calculating between 2 dates
 
Similar Threads
date formate
Date - calulate new to x number of months?
Format date using jsp to save into mysql DB
Problem with age script
refresh/reload Netscape