Web Analytics Made Easy -
StatCounter Calculate duration - CodingForum

Announcement

Collapse
No announcement yet.

Calculate duration

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calculate duration

    Is there anyone who has JavaScript code to calculate the duration in minutes from one date and time to another date and time.
    i.e. 06/21/02 11:23 to 06/26/02 23:30
    Thanks,
    Ed

  • #2
    This gives days, hours, and minutes spelled out:

    From=document.forms[0].From.value; // as MM/DD/YYYY HH:MM
    To=document.forms[0].To.value;
    FM=From.substring(0,2)-1; // from month (0-11)
    FD=From.substring(3,5); // ...day
    FY=From.substring(6,10); // ...year
    FH=From.substring(11,13); // ...hours
    FN=From.substring(14,16); // ...minutes
    TM=To.substring(0,2)-1;
    TD=To.substring(3,5);
    TY=To.substring(6,10);
    TH=To.substring(11,13);
    TN=To.substring(14,16);
    FX=new Date(FY,FM,FD,FH,FN,0);
    TX=new Date(TY,TM,TD,TH,TN,0);
    Diff=TX-FX; // difference in miliseconds
    Diff=Math.round(Diff/(1000*60)); // minutes
    DD=Math.floor(Diff/(60*24)); // full days
    Diff=Diff-DD*60*24;
    DH=Math.floor(Diff/60); // full hours
    DN=Diff-DH*60; // minutes
    Diff=DD+' days '+DH+' hours '+DN+' minutes';
    document.forms[0].Diff.value=Diff;

    Drop all calculations after the "Diff=Math.round(Diff/(1000*60));" line to get straight minutes.

    Visit the "Samples of my work > Days between dates" page at http://spruce.flint.umich.edu/~jalarie/ for more fun with dates and times.
    Having said that, I've probably told you more than I know.

    Comment

    Working...
    X