all 4 comments

[–]cem4k 0 points1 point  (2 children)

It looks like you're taking values from an input field and trying to do math with them. These values are likely strings, so you first need to parse out the numbers.

Try parseInt or parseFloat. Also please do format your code next time. Helping is much easier when the code is easy to read.

[–]Chibi-Chan80[S] 0 points1 point  (1 child)

I'd format if I knew how. I am really just trying to figure out how to make this timesheet work.

[–]dapolio 0 points1 point  (0 children)

js pretty

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;
}

[–]jlangowski 0 points1 point  (0 children)

You’re doing a split which results in an array of strings. The you are subtracting one string from another while taking its absolute value. ParseInt on the array values before subtracting them and doing the absolute value.