Results 1 to 2 of 2

Thread: How to calculate time between two dates

  1. #1
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to calculate time between two dates

    Hi,

    i'm trying to calculate the time between two dates. I already searched on internet, and i always see the same answer.

    But if i do it, it's not right...

    For example:
    Code:
    Current date = 2009,6,19
    date = 2009,7,22
    
    var date1=new Date(19,5,2009);
    var date2=new Date(22,6,2009)
    //I do -1 because in javascript: January is 0 and December 11
    
    date2 = -1325638800000
    date1 = -1420333200000
    Dif: 1096
    = Math.abs((date1.getTime()-date2.getTime())/(one_day)
    What is wrong?

  2. #2
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    Have a look at this code I found

    Code:
            t1="10/10/2006" ;
    
            t2="15/10/2006";
    
     
       //Total time for one day
            var one_day=1000*60*60*24; 
    //Here we need to split the inputed dates to convert them into standard format
    for furter execution
            var x=t1.split("/");     
            var y=t2.split("/");
      //date format(Fullyear,month,date) 
    
            var date1=new Date(x[2],(x[1]-1),x[0]);
      
            var date2=new Date(y[2],(y[1]-1),y[0])
            var month1=x[1]-1;
            var month2=y[1]-1;
            
            //Calculate difference between the two dates, and convert to days
                   
            _Diff=Math.ceil((date2.getTime()-date1.getTime())/(one_day)); 
    //_Diff gives the diffrence between the two dates.
    Last edited by forum_amnesiac; 06-19-2009 at 12:57 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •