The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

function timeDiff

I hope you can help me with a problem on a function which should work properly but isnít.

The function is:

function timeDiff(time1, time2) {

if(isNaN(Date.parse(time1)) || isNaN(Date.parse(time2))) return '';

return ( ((Date.parse(time2) -Date.parse(time1))/1000)/60 )
}

The problem is that if I set time1 to say 25/05/11 and time2 to say 26/05/2011 the result should be 1440 (the number of minutes in I day) however the result I am receiving is 44640 (the number of minutes in 31 days)

I have check the date formats on the server and the dbase and they are all set to the UK format of dd/mm/yyyy

Re: function timeDiff

Can you post an SSCCE that demonstrates the problem? There are 2 Date classes (java.util.Data and java.sql.Date which extends the util version), and the parse method is deprecated (you should be using the DateFormat class)

Re: function timeDiff

i have no idea what you mean by SSCCE and java.util.Data and java.sql.Date

First, see my signature for a definition of an SSCCE. Second, I recommend you become familiar with the API of classes you are using - in this case here is the API for java.sql.DateDate (Java Platform SE 6)
Find the parse method, and note the deprecated annotation and the suggestion of what to use. If you just want the number of days, you could substract the time in milliseconds of each Date and compute from there the number of days with a little math (1000 milliseconds in a second, 60 seconds in a minute, etc...)

Re: function timeDiff

Originally Posted by dlorde

Wrong language error in line 1.

Since when did Java declare methods like this: function timeDiff(time1, time2) ?

This looks like Javascript to me.

Doh! Good eye dlorde. bobby321, there is a big difference between java and javascript - if this is javascript you are after, you might try your luck at a more dedicated javascript forum - although the technique of getting the Date in milliseconds I described above still holds true.