These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Calculating difference between date and time

jpmeyer8116
Registered: Sep 29 2009
Posts: 12

I am trying to calculate the total time is takes for a document to be reviewed. I have a field where the user needs to type in the start review date and time, have another field where the user needs to type in the stop review date and time. Not sure how I go about calculating the total review time.

I have reviewed similiar forums but they are either dealing with only calculating the difference of days between dates or difference of time. Not both.....I'm really stumped on this one. Any advice would be greatly appreciated!

Example the review start was 10/6/09 at 10:35am and the review stop was at 10/8/09 at 4:00PM, how is the total time calculated?

My Product Information:
Acrobat Standard 7.0.9, Windows
jpmeyer8116
Registered: Sep 29 2009
Posts: 12
Luckily with further user definition, they only want to calculate the number of days between dates...I have been able to figure this out by previous forums.

Thanks!
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
First have you reviewed Thom Parker's series on [url=http://www.acrobatusers.com/learning_center/tutorials?title=time&tid_1=6&form_build_id=form-3174ea2f21c03503d60a0d9300383476&form_id=views_exposed_form]Working with Date and Time in Acrobat JavaScript]?Acrobat uses JavaScript date object for date and time manipulation.

The first thing you need to do is convert the date and time to a JS date object for the end and start. Then you can convert each date object to the number of milliseconds since the Epoch date. And with those values you can compute the time in milliseconds. you can then use some simple math to convert the difference to what ever units you want.

// create start time stringvar sStart = '10/6/09 10:35 am'; // start date and time// create end time stringvar sEnd = '10/8/09 4:35 pm'; // end date and time// convert strings to date objectsvar oStart = util.scand('mm/d/yy h:MM t', sStart); // strat objectvar oEnd = util.scand('mm/d/yy h:MM t', sEnd); // end object// convert objects to numbervar fStart = oStart.getTime();var fEnd = oEnd.getTime();// compute difference in millisecondsvar fDiff = fEnd - fStart;// convert to time unitsvar fSecs = fDiff / 1000; // seocndsvar fMins = fSecs / 60; // minutesvar fHrs = fMins / 60; // hoursvar fDays = fHrs / 24; // days // display some values:console.println('start time: ' + sStart + ' end time: ' + sEnd);console.println('length of time in minutes: ' + fMins);console.println('length of time in hours: ' + fHrs);console.println('length of time in days: ' + fDays); // convert to a single formatted string of days hours:minutesvar fWholeMins = Math.round(fMins) % 60; // rounded minutesvar fWholeHrs = Math.floor(fHrs) % 24;var fWholeDays = Math.floor(fDays); // turncted daysconsole.println('review time ' + fWholeDays + ' days ' + fWholeHrs + ' hours and ' + fWholeMins + ' minutes');

George Kaiser

jpmeyer8116
Registered: Sep 29 2009
Posts: 12
Thanks for the reply...that will definitely help in the future when I do need to calculate based off date and time.

I still am having one simple (maybe simple problem)...My code is below and it works beautifully, however I'm not sure of where or how I can state that if the RevStart date and RevStop date are equal that the event.value equals 1.

var strStart = this.getField("txtRevStart").value;
var strStop = this.getField("txtRevStop").value;
if(strStart != "" & strStop != "")
{
var dateStart = util.scand("mm/dd/yyyy",strStart);
var dateStop = util.scand("mm/dd/yyyy",strStop);
var diff = dateStop.getTime() - dateStart.getTime();
// One Day = (24 hours) x (60 minutes/hour) x
// (60 seconds/minute) x (1000 milliseconds/second)
var oneDay = 24 * 60 * 60 * 1000;
var days = Math.floor(diff/oneDay);
event.value = days;
}
else{
event.value= ""}