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 4 of 4

Thread: JAVASCRIPT DATE DIFFERENCE PROBLEM

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Location
    Kochi,India
    Posts
    18
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation JAVASCRIPT DATE DIFFERENCE PROBLEM

    First of all, sorry if this post is in wrong forum. I could not find a forum here for js. so posting it here. My problem is that i've written a js function to find the difference between two dates. the format being used is dd/mm/yyyy hh:mm. The function returns correct value when give one set of values, but go wrong with another set. examples are given below.

    set 1 : Time 1 = 24/02/2011 09:30 , time 2 = 24/02/2011 16:00

    Output is corret here. It gives 6 Hours & 30 Minutes (after converting the difference)

    set 2: Time 1 = 24/02/2011 09:30 , time 2 = 25/02/2011 16:00

    Here, it gives 31 days, 6 Hours & 30 Minutes. My code is given below. Also the alert of dates display strange values. Don't know if it is timezone issue. I wonder what is going wrong here

    function compareDateTime(frmtime,totime)
    {
    	var date1 = new Date(frmtime);
    	var date2 = new Date(totime);
    	var diff = new Date();
    	alert(date1);
    	alert(date2);
    	diff = (date2.getTime()-date1.getTime());
    	if (diff<0)
    	{
    		alert("From Time cannot be later than To Time!");
    		return 0;
    	}
    	else if (diff==0)
    		{
    			alert("From Time cannot be equal with To Time!");
    			return 0;
    		}
     
    	else
    	{
    		return diff;
    	}
    }

    The returned diff value is broken down as following:
    if (diff>0)
    	{
    		days = Math.floor(diff / (1000 * 60 * 60 * 24));
     		diff -= days * (1000 * 60 * 60 * 24);
     		hours = Math.floor(diff / (1000 * 60 * 60));
     		diff -= hours * (1000 * 60 * 60);
     		mins = Math.floor(diff / (1000 * 60));
     		alert(days+","+hours+","+mins);
    		return true;
    	}

    Please Help...


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: JAVASCRIPT DATE DIFFERENCE PROBLEM

    Java and Javascript are quite different. Try doing a search on "Javascript forum" and you'll find a plethora of forums which may be able to better answer your question (unfortunately, I don't know too much about good Javascript forums, though StackOverflow is a very good site for pretty much anything programming related).

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: JAVASCRIPT DATE DIFFERENCE PROBLEM

    Your second part of code is not correct. And you have made it too difficult to understand. Why don't you try simple like;
    HTML Code:
    var minutes=1000*60;
    var hours=minutes*60;
    var days=hours*24;
    var years=days*365;
    var d=new Date();
    var t=d.getTime();

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: JAVASCRIPT DATE DIFFERENCE PROBLEM

    Please note that this is a Java forum and not a JavaScript forum.

    I have moved this thread to - Other Programming Languages

    This in no way guarantees a valid answer though. The Other Programming Languages forum is a dump for threads like these.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. javascript problem
    By kundan_101 in forum Member Introductions
    Replies: 2
    Last Post: December 30th, 2010, 09:02 AM
  2. [SOLVED] Difference between == and equals.
    By goldest in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 18th, 2010, 03:15 PM
  3. Replies: 1
    Last Post: August 13th, 2010, 06:58 AM
  4. sql date problem
    By realosso in forum JDBC & Databases
    Replies: 2
    Last Post: June 3rd, 2010, 08:32 AM
  5. Need to know this difference
    By arvind in forum Java Theory & Questions
    Replies: 2
    Last Post: January 19th, 2010, 06:24 AM