Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.

Adding up hours fields - error

Avatar

Former Community Member

I have the following code that is to add the hours & minutes in a field.

This code works however I get the error NaN:NaN in the totals field if I enter any time with no minutes under 24:00. so if I enter 23:00, 22:00, 01:00 then I get this error. If I enter 24:00, 25:00 + it works?

var mins = new Array()

var minsTotal = 0;

if (Row2.Monday.rawValue != null){

    mins = Row2.Monday.rawValue.split(":")

    minsTotal += parseInt((parseInt(mins[0]) * 60) + parseInt(mins[1]))

}

if (Row3.Tuesday.rawValue != null){

    mins = Row3.Tuesday.rawValue.split(":")

    minsTotal += parseInt((parseInt(mins[0]) * 60) + parseInt(mins[1]))

}

if (Row4.Wednesday.rawValue != null){

    mins = Row4.Wednesday.rawValue.split(":")

    minsTotal += parseInt((parseInt(mins[0]) * 60) + parseInt(mins[1]))

}

if (Row5.Thursday.rawValue != null){

    mins = Row5.Thursday.rawValue.split(":")

    minsTotal += parseInt((parseInt(mins[0]) * 60) + parseInt(mins[1]))

}

if (Row6.Friday.rawValue != null){

    mins = Row6.Friday.rawValue.split(":")

    minsTotal += parseInt((parseInt(mins[0]) * 60) + parseInt(mins[1]))

}

this.rawValue = parseInt(minsTotal/60) + ":" + parseInt(minsTotal%60);



3 Replies

Avatar

Level 10

Hi, You have to be careful using parseInt as the default behaviour when there is a leading zero is to treat the value as octal, so parseInt(“08”) will return zero (because it is an invalid octal number).  You should always specify the radix in the optional second parameter, so use parseInt(“08”,10) this means parseInt knows it is working in base 10.  But I was not able to reproduce your problem unless I entered a value without the colon.  Maybe you can try something like this instead, which only adds values that fit the correct format,

var timeRegex = /(\d\d):(\d\d)/;

var minsTotal = 0;

var d = timeRegex.exec(Row2.Monday.rawValue);

if (d !== null)

{

    minsTotal += parseInt(d[1],10) * 60 + parseInt(d[2],10);

}

var d = timeRegex.exec(Row3.Tuesday.rawValue);

if (d !== null)

{

    minsTotal += parseInt(d[1],10) * 60 + parseInt(d[2],10);

}

var d = timeRegex.exec(Row4.Wednesday.rawValue);

if (d !== null)

{

    minsTotal += parseInt(d[1],10) * 60 + parseInt(d[2],10);

}

var d = timeRegex.exec(Row5.Thursday.rawValue);

if (d !== null)

{

    minsTotal += parseInt(d[1],10) * 60 + parseInt(d[2],10);

}

var d = timeRegex.exec(Row6.Friday.rawValue);

if (d !== null)

{

    minsTotal += parseInt(d[1],10) * 60 + parseInt(d[2],10);

}

var minutes = minsTotal % 60;

this.rawValue = parseInt(minsTotal / 60, 10) + ":" + ((minutes < 10) ? "0" + minutes : minutes);

I hope this helps, Bruce

Avatar

Former Community Member

Thanks for your reply. I copied in your code but the field does not seem to add anything. it stays as zero?

I should also add that i copied my fields and code into a new blank form and my problem went away.

Would it throw errors if these fields are in a table?

Avatar

Level 10

Hi,

Glad to hear you got it working, this is a link to the form I used to check the code above, https://acrobat.com/#d=2M1Mp*JFh5QpoXQuOOH24g, if that still helps.

Being in a table might change the field reference but might not, depends on how it is structured, so could work without change or with a minor change to the Row1.Monday (and the like) references.

Regards

Bruce