Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Math calculation problem! - Where is the help? ? ?

Avatar

Former Community Member
I posted the message below hoping someone would be able to help - so far nothing!



For sure there must be someone out there that can open the designer, create three numeric boxes, set the objects to be time oriented only, and the last box having the appropriate Java script or FormCalc script to have it show the difference in time.



The script I show below works just great - accept as noted. WHY? What modifications do I need to make to have it work 100% of the time?



<<<< below is the orginal posting on the forum >>>>>>>>>>



The following Java script is entered in as the "total time" calculation formula



"this.rawValue = endingTime.rawValue - beginningTime.rawValue" however when the following times are inserted the calculation accuracy fails.



beginning time: 1430

ending time: 1700



the total time becomes 2:70 rather than 2:30.



The reason seems to be the time format in PM mode is reading as a numeric formula rather than a time formula. Therefore, the subtraction is standard borrowing and carrying (1700 becomes 1600 + 100 + 0) rather than time (1700 becomes 1600 + 60).



Does anyone have a solution?



I have also tried resolving the issue using "FormCalc" but with no success



Please help!



thanks,



tom
5 Replies

Avatar

Former Community Member
ok well friend your problem is with the way things are setup you cant just subtract a number and expect it to work in time format.

Avatar

Former Community Member
the accuracy is tottaly correct 1700 -1430 is 270 it is that simple however what your trying to do is subtract the milliseconds of one time from another and convert it back into that many milliseconds divided up into hours and minutes. So calm down and get the formcal refrence manual to help it is poorly written but it is a starting point.

I think you want to do this set both of the fields startDate and EndDate to time/date fields this way you can lock the way information is entered set the display validation and binding patteren to HH:MM then set the binding tab to time from here your final box can be a text field or a date field doesnt matter so much then the easiest way to do this in Livecycle is to use form calc on the total time field

set the script to formcalc and place it under the calculation event.

if(startDate.isNull or endDate.isNull) \\ keep calculation clean

then

$ = 1 \\incase used with another calculation no errors

else

$=Time2Num(startDate.formattedValue, "HH:MM")-Time2Num(endDate.formattedValue, "HH:MM")

endif

you may have to do some tweaking to get the number that comes out as the format you want but this should lead you down the right path the Time2Num can be reversed using Num2Time also

good luck

Avatar

Former Community Member
This solution almost worked.



I had to make the following changes and it finally worked.



I put what Puta Eka advised:



if(startDate.isNull or endDate.isNull)

then

$ = 0

else

$=Time2Num(startDate.formattedValue, "HH:MM")-Time2Num(endDate.formattedValue, "HH:MM")

endif



in the calculation of a "hidden" date/time standard object.



Then in the "shown" date/time object I added the following calculation script.



$= form1.#subform1.difference / (60 * 60 * 1000)



I tried putting the (60 * 60 * 1000) in with the formula from Puta Eka, but it just wouldn't work.



Anyway, between his kind and considerate help and what I did, that problem was solved.



On to the next problem!



:)

Avatar

Former Community Member
I had a lot of trouble with this too, thanks for the help. My solution also adds days for a leave form. The key for me was using the .formattedValue attribute and making sure the binding and value types for the time fields matched "HH:MM". My edit and display formats were "h:MM A".



on the exit event:



if ( HasValue(FromDate) and HasValue(ToDate) and HasValue(FromTime) and HasValue(ToTime) ) then

if ( FromDate == ToDate ) then

Total_Hours = 8 - ((Time2Num(FromTime.formattedValue, "h:MM A" ) - Time2Num("8:00 AM", "h:MM A" ) ) + ( Time2Num("5:00 PM", "h:MM A" ) - Time2Num(ToTime.formattedValue, "h:MM A" ) ) ) / (1000 * 60 * 60)

else

var PartialDayHours = ((Time2Num(FromTime.formattedValue, "h:MM A" ) - Time2Num("8:00 AM", "h:MM A" ) ) + ( Time2Num("5:00 PM", "h:MM A" ) - Time2Num(ToTime.formattedValue, "h:MM A" ) ) ) / (1000 * 60 * 60)

Total_Hours = 8 + 8 * ( Date2Num(ToDate, "YYYY-MM-DD") - Date2Num(FromDate, "YYYY-MM-DD") ) - PartialDayHours

endif

endif



Hope this saves somebody else some time...

Avatar

Level 7
For many calculations, you need to check that you have the necessary fileds before calculating. For date and time calculations, the exact format of the data string is needed to correctly convert the date. For example Jan 3, 2009 can be written like "01/03/2009" ("MM/DD/YYYY" or 03/01/2009 ("DD/MM/YYYY") and using the wrong formatting string will get you the values for Mar 1, 2009.