Time Difference Calculation with an Exception

New Here ,
Jan 07, 2019

Copy link to clipboard

Copied

I am working on a vacation request form that has a field for start time and end time in h:mm tt format.  I have a script that is working for calculating the difference in those fields, however, I need this have an exception and I am not sure what to add/change.

If an employee request 8:00 am to 12:00 pm the script works fine, however, if they request 8:00 am to 5:00 pm it fails because it doesn't take the hour lunch period into consideration.

Please advise.

// get the values of the fields

var sStart = this.getField("Start TimeRow1").value;

var sEnd = this.getField("End TimeRow1").value;

// clear field as a default action

event.value = "";

// compute only if we have values

if(sStart != "" && sEnd != "") {

// add a formatted date value to the time field values

sStart = "1-Jan-1970 " + sStart;

sEnd = "1-Jan-1970 " + sEnd;

// convert the date time strings to date object

var oStart = util.scand("d-mmm-yyyy h:MM tt", sStart);

var oEnd = util.scand("d-mmm-yyyy h:MM tt", sEnd);

// convert date object to milliseconds since 1-Jan-1970

var nStart = oStart.getTime();

var nEnd = oEnd.getTime();

// compute the difference in milliseconds

var nDiff = nEnd - nStart;

// convert to seconds

var nSeconds = Math.floor(nDiff / 1000);

// compute the whole hours from the minutes

var nHrs = Math.floor(nSeconds / (60 * 60));

// compute the remaining minutes

var nMins = (nSeconds / 60) % 60;

// set field value string to formatted time string

event.value = nHrs + ":" + util.printf("%,202.0f", nMins);

} // end have values

TOPICS
Acrobat SDK and JavaScript, Windows

Views

66

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Time Difference Calculation with an Exception

New Here ,
Jan 07, 2019

Copy link to clipboard

Copied

I am working on a vacation request form that has a field for start time and end time in h:mm tt format.  I have a script that is working for calculating the difference in those fields, however, I need this have an exception and I am not sure what to add/change.

If an employee request 8:00 am to 12:00 pm the script works fine, however, if they request 8:00 am to 5:00 pm it fails because it doesn't take the hour lunch period into consideration.

Please advise.

// get the values of the fields

var sStart = this.getField("Start TimeRow1").value;

var sEnd = this.getField("End TimeRow1").value;

// clear field as a default action

event.value = "";

// compute only if we have values

if(sStart != "" && sEnd != "") {

// add a formatted date value to the time field values

sStart = "1-Jan-1970 " + sStart;

sEnd = "1-Jan-1970 " + sEnd;

// convert the date time strings to date object

var oStart = util.scand("d-mmm-yyyy h:MM tt", sStart);

var oEnd = util.scand("d-mmm-yyyy h:MM tt", sEnd);

// convert date object to milliseconds since 1-Jan-1970

var nStart = oStart.getTime();

var nEnd = oEnd.getTime();

// compute the difference in milliseconds

var nDiff = nEnd - nStart;

// convert to seconds

var nSeconds = Math.floor(nDiff / 1000);

// compute the whole hours from the minutes

var nHrs = Math.floor(nSeconds / (60 * 60));

// compute the remaining minutes

var nMins = (nSeconds / 60) % 60;

// set field value string to formatted time string

event.value = nHrs + ":" + util.printf("%,202.0f", nMins);

} // end have values

TOPICS
Acrobat SDK and JavaScript, Windows

Views

67

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jan 07, 2019 0
Most Valuable Participant ,
Jan 07, 2019

Copy link to clipboard

Copied

What's the logic behind it? If it's more than X hours reduce one hour? If it includes 12:00-1:00 reduce one hour? etc.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 07, 2019 0
New Here ,
Jan 07, 2019

Copy link to clipboard

Copied

Yes, the logic would be if the total exceeds 4 hours then deduct 1 hour.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 07, 2019 1
try67 LATEST
Most Valuable Participant ,
Jan 07, 2019

Copy link to clipboard

Copied

OK, then after this line of the code:

var nSeconds = Math.floor(nDiff / 1000);

Add this:

if (nSeconds>14400) nSeconds-=3600;

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 07, 2019 0
Adobe Community Professional ,
Jan 07, 2019

Copy link to clipboard

Copied

This is what can happen when one simply copies and pastes code without any understanding of code or the function of the code. If this were computing the time of an airplane, bus, or train trip the calculation would be correct for the involved time interval for the trip.

What was the context of the original post?

Seems like that how it the time interval works in the real world, unless one adds a field for the time taken for lunch or the start and end times of the lunch break.

How do you want to proceed?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 07, 2019 0