Tip

Showing elapsed time in web views without using Today

If you use @today or @now function in view columns or selection formulas, it will result in noticable performance problem on server.

In Web environment, we have a powerful tool: javascript...

We will have a view with a field StartDateTime and we are going to show the time between now and StartDateTime.

Code is self-explanatory...


 <SCRIPT>
function showElapsed() {

// **************************************************************
//  Display elapsed time at the web client
//  (C) Serdar Basegmez
//  sbasegmez%yahoo.com   (anti-spam precaution :)))
// **************************************************************
// 1-->
// dateadjustment is a variable that needs to be computed at the top of form. It shows us the difference of server's now and browser's now. We
// avoid negative elapsed times with the variable. At the top of template of the view ($$View Template of XXX), place a computed-for-display 
// field with the following formula. This notation is used to avoid date format conflicts...
//
//    yr:=@Text(@Year(@Now));
//    mnt:=@Text(@Month(@Now)-1);
//    dy:=@Text(@Day(@Now));
//    hr:=@Text(@Hour(@Now));
//    mn:=@Text(@Minute(@Now));
//    sc:=@Text(@Second(@Now));
//    "[<SCRIPT>var dayAtServer=new Date("+yr+"," +mnt+ "," +dy+ "," + hr+ "," +mn + "," +sc+ "); 
//     var dateadjustment=((new Date())-dayAtServer)"+"</SCRIPT>]"

// 2 -->
// You should place a declaration in JS Header of template of the view:
//
// var dates=new Array();

// 3-->
// You should place a column in a view that has a formula: It creates div's with unique names. The code set inner html's of div's according to related
// member of dates array. You should beware of replication conflicts and response documents... I have excluding these documents in the view.
//
// ourdate:=StartDateTime;
// yr:=@Text(@Year(ourdate));
// mnt:=@Text(@Month(ourdate)-1);
// dy:=@Text(@Day(ourdate));
// hr:=@Text(@Hour(ourdate));
// mn:=@Text(@Minute(ourdate));
// sc:=@Text(@Second(ourdate));
// order:=@Text(@DocNumber(""));
//
// "[<div ID='ElapsedTime"+order+"'></DIV><SCRIPT>dates["+order+"]=new Date("+yr+"," +mnt+ "," +dy+ "," + hr+ "," +mn + "," +sc+ ");</SCRIPT>]"


var datenow=(new Date())-dateadjustment;  // computes date of the server.
var i=1;

while (dates[i]!=null)  // Loop until the last doc...
{
var elapsed=0;
elapsed=Math.floor((datenow-dates[i])/60000);  //compute elapsed time in minutes. This can be replaced... division by 1000 gives time in seconds.
showdt="";


if (elapsed>=1440) {
 showdt+=String(Math.floor(elapsed/1440))+"d:";   //compute # of days...
 elapsed=elapsed%1440;
}
if (elapsed>=60) {
 showdt+=String(Math.floor(elapsed/60))+"h:";   // compute # of hours
 elapsed=elapsed%60;
} 
if (elapsed!=0) {
 showdt+=String(Math.floor(elapsed))+"m";   //compute # of minutes
}

document.getElementById("ElapsedTime"+i).innerHTML = "<Center>"+showdt+"</Center>"; //place the element...
i++;
}

counter=setTimeout("showElapsed()",1000);  //I am using this command to refresh the elapsed times each second. I am not using it for large views.
}

</SCRIPT>

<SCRIPT>
showElapsed();   //initiate the function...
</SCRIPT>
  

This was first published in August 2002

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.