Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: function timeDiff

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default function timeDiff

    Hi forum

    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

    My data connection is SQL

    Any ideas ?

    Thanks

    bobby


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default 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)

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: function timeDiff

    Hi copeg

    Thanks for the prompt reply, I am new to java and its workings so please can you give more details as i have no idea what you mean by SSCCE and java.util.Data and java.sql.Date

    I am trying to do a calculation between to date fields in a data table the data connection is SQL and all my date formats in the table are set to the UK format of dd/mm/yyyy.

    Thanks
    Bobby

  4. #4
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: function timeDiff

    Hi again copeg

    Is there a standard function I can use and call which calculates / returns the days between to dates which is what I am trying to do.

    Thanks Again

    Bobby

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default 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.Date
    Date (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...)

  6. #6
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: function timeDiff

    Hi again copeg

    I will read up on your suggested material

    Thanks

  7. #7
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: function timeDiff

    Wrong language error in line 1.

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

    This looks like Javascript to me.

  8. The Following User Says Thank You to dlorde For This Useful Post:

    copeg (May 25th, 2011)

  9. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: function timeDiff

    Quote Originally Posted by dlorde View Post
    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.

Similar Threads

  1. I Cant seem to get my sort routine to function
    By rrickman9 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 22nd, 2011, 07:48 PM
  2. About the ackermann function...
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2011, 09:24 AM
  3. Need Help on the Interface Function
    By yel_hiei in forum Object Oriented Programming
    Replies: 12
    Last Post: July 29th, 2010, 07:27 AM
  4. Substring function
    By bristol580 in forum Java SE APIs
    Replies: 2
    Last Post: November 12th, 2009, 10:29 AM
  5. Function of difference between two numbers
    By uplink600 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 13th, 2009, 05:57 AM