Search these areas:
Help
Support Forums
Product Info



-Introduction
-DataWeb: What's New
-Creating an Application
-The DataWeb Designer
-Application Management
-Data Tables
-Data Tables
-Views
-Templates
-Components
-Data Analysis
-Members
-Security
-Importing
-Exporting
-FTP
-JavaScript
-Object Model
-Full-Text Search
-Domain and Email Hosting
-HotBlock Modules
-Account Management
-Glossary
-New Responsive UI Module





DataWeb Help
Support Forums
Tutorial
Script Library
Knowledge Base

Home > Support

Script Library

  Find elapsed time between two dates
 
  chubbard posted this script:
  This function takes two dates and prints the elapsed time between them in days, hours, and minutes.

For example, if you pass in "3/19/2001 7:00" for the start date and "3/23/2001 18:30" for the end date, the function returns the following string:

4 day(s) 11 hour(s) 30 minute(s)

webcall function calcElapsedTime(dStart, dEnd)
{
    dStart = new Date(dStart);
    dEnd = new Date(dEnd);

    //reverse arguments if end is earlier than start.
    if(dStart > dEnd)
        {
        var dTemp = dStart;
        dStart = dEnd;
        dEnd = dTemp;
        }
   
    //find total number of milliseconds elapsed.
    var nMs = Number(dEnd - dStart);
    var nDays = 0;
    var nHours = 0;
    var nMinutes = 0;
    var nSeconds = 0;
    var szReturn = "";

    //find number of whole days elapsed.   
    nDays = Math.floor(nMs / (1000 * 60 * 60 * 24));
    if(nDays >= 1)
        szReturn = nDays + " day(s) ";

    //determine remainder (in milliseconds) after whole days are accounted for.
    nHours = nMs - (nDays * 1000 * 60 * 60 * 24);
    //determine number of whole hours in remainder.
    nHours = Math.floor(nHours / (1000 * 60 * 60));   
    if(nHours >= 1)
         szReturn += nHours + " hour(s) ";

    //determine remainder (in milliseconds) after whole days and hours are accounted for.
    nMinutes = nMs - ((nDays * 1000 * 60 * 60 * 24) + (nHours * 1000 * 60 * 60));
    //determine number of whole minutes in remainder.
    nMinutes = Math.floor(nMinutes / (1000 * 60));
    if(nMinutes >= 1)
         szReturn += (Math.floor(nMinutes) + " minute(s) ");

    //print out string showing elapsed time.
    return szReturn;
}
 
  on 2001-04-20 20:45:00, chubbard responded:
  Here's my take on this function.  I stole some of the above code, but tried to let the date/string methods do some more of the work for me.

public function formatElapsedTime(start, end)
    {
    var str = "";
    var elapsed = Date(end).valueOf() - Date(start).valueOf();

    var nDays = Math.floor(elapsed / (1000 * 60 * 60 * 24));
    if (nDays)
        szReturn = nDays + " day(s) ";

    // hours:minutes:seconds (.toString() loses milliseconds!)
    str += elapsed.toString().substring(11);

    var nMS = elapsed.getMilliseconds();
    str += (nMS/1000).toString().substring(1);

    return str;
    }
 
Affiliate | Partner | Terms of Use | Privacy Policy | Contact Us | Pricing | Bring DataWeb In-House    
DataWeb, 720 North 10th Street, A #145, Renton, Washington 98057 *425-583-5970* Fax 484-770-4706* Email Us