Highlighted

Another Calculation Question

New Here ,
Oct 26, 2020

Copy link to clipboard

Copied

Hello All, 

 

I am working on a timesheet that has In/Out In/Out and am having NaN errors with my code. This is the first time I've tried this so I am going at it blind. 

 

image.png

 

The DOCUMENT JavScript is:

function Time2Num(sFormat, sTime) {

if(sTime == '') return ''; // exit

// get date time for Epoch date and sTime

var oTime = util.scand('mm/dd/yyyy ' + sFormat, '01/01/1970 ' + sTime);

// convert UTC Offset to milliseonds for adjustment

var fTZOffset = oTime.getTimezoneOffset() * 1000 * 60

// time since the start of the day in millseconds

var fTime = oTime.valueOf() - fTZOffset;

// convert to seconds and return value

return Math.round(fTime / 1000);

}

function TimeDiff(cStartField, cEndField) {

var sTimeFormat = 'hh:mm';

var fDiff = 0;

// get the start time

var sStart = this.getField(cStartField).value;

// get the end time

var sEnd = this.getField(cEndField).value;

// complete script only if we have data

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

// convert sStart string to seconds

var fStart = Time2Num(sTimeFormat, sStart);

// convert sEnd string to seconds

var fEnd = Time2Num(sTimeFormat, sEnd);

if (fEnd<fStart) fEnd+=86400;

// compute difference in seconds

fDiff += fEnd - fStart;

}

return fDiff;

}

 

The NaN error is showing with this custom calculation script:

 

var start = this.getField("InSunday").value;
var finish = this.getField("OutSunday_2").value;
if (start=="" || finish=="") event.value = "";
else {
var lunchout = this.getField("OutSunday").value;
var lunchin = this.getField("InSunday_2").value;
var startArr = start.split(":");
var finishArr = finish.split(":");
var hourWDiff = Math.abs(finishArr[0] - startArr[0]);
var minWDiff = (Math.abs(finishArr[1] - startArr[1])/60*100);
var hourLDiff = 0;
var minLDiff = 0;
if (lunchout!="" && lunchin!="") {
var lunchoutArr = lunchout.split(":");
var lunchinArr = lunchin.split(":");
hourLDiff = Math.abs(lunchoutArr[0] - lunchinArr[0]);
minLDiff = (Math.abs(lunchoutArr[1] - lunchinArr[1])/60*100);
}
var totalHrs = hourWDiff + (minWDiff/60) + hourLDiff + (minLDiff/60);
event.value = totalHrs;
}

 

Any help would be appreciated! 

 

[Question moved to the Acrobat forum by moderator]

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

18

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

Another Calculation Question

New Here ,
Oct 26, 2020

Copy link to clipboard

Copied

Hello All, 

 

I am working on a timesheet that has In/Out In/Out and am having NaN errors with my code. This is the first time I've tried this so I am going at it blind. 

 

image.png

 

The DOCUMENT JavScript is:

function Time2Num(sFormat, sTime) {

if(sTime == '') return ''; // exit

// get date time for Epoch date and sTime

var oTime = util.scand('mm/dd/yyyy ' + sFormat, '01/01/1970 ' + sTime);

// convert UTC Offset to milliseonds for adjustment

var fTZOffset = oTime.getTimezoneOffset() * 1000 * 60

// time since the start of the day in millseconds

var fTime = oTime.valueOf() - fTZOffset;

// convert to seconds and return value

return Math.round(fTime / 1000);

}

function TimeDiff(cStartField, cEndField) {

var sTimeFormat = 'hh:mm';

var fDiff = 0;

// get the start time

var sStart = this.getField(cStartField).value;

// get the end time

var sEnd = this.getField(cEndField).value;

// complete script only if we have data

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

// convert sStart string to seconds

var fStart = Time2Num(sTimeFormat, sStart);

// convert sEnd string to seconds

var fEnd = Time2Num(sTimeFormat, sEnd);

if (fEnd<fStart) fEnd+=86400;

// compute difference in seconds

fDiff += fEnd - fStart;

}

return fDiff;

}

 

The NaN error is showing with this custom calculation script:

 

var start = this.getField("InSunday").value;
var finish = this.getField("OutSunday_2").value;
if (start=="" || finish=="") event.value = "";
else {
var lunchout = this.getField("OutSunday").value;
var lunchin = this.getField("InSunday_2").value;
var startArr = start.split(":");
var finishArr = finish.split(":");
var hourWDiff = Math.abs(finishArr[0] - startArr[0]);
var minWDiff = (Math.abs(finishArr[1] - startArr[1])/60*100);
var hourLDiff = 0;
var minLDiff = 0;
if (lunchout!="" && lunchin!="") {
var lunchoutArr = lunchout.split(":");
var lunchinArr = lunchin.split(":");
hourLDiff = Math.abs(lunchoutArr[0] - lunchinArr[0]);
minLDiff = (Math.abs(lunchoutArr[1] - lunchinArr[1])/60*100);
}
var totalHrs = hourWDiff + (minWDiff/60) + hourLDiff + (minLDiff/60);
event.value = totalHrs;
}

 

Any help would be appreciated! 

 

[Question moved to the Acrobat forum by moderator]

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

19

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
Oct 26, 2020 0

Have something to add?

Join the conversation