Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

time calculation

Avatar

Level 2

Hi, I am trying to do a time calculation

If

Time is between 12:01 am -6:00 am then I would like the calculation to = 1

Time is between 6:01 am - 12:00 pm then I would like the calculation to = .75

Time is between 12:01 pm - 6:00 pm then I would like the calculation to = .50

Time is between 6:01 pm to 12:00 am then I would like the calculation to = .25

I have looked at a lot of forums and I just cannot get this to work. I am trying to calculate travel time. 

Please help.

sara

9 Replies

Avatar

Former Community Member

Doing time calculations is never as easy as it seems. There are functions provided in FormCalc that make it easier to do but basically getting a number to represent the time is needed to do comparisons. There us a Time2Num function that gets you the number of milliseconds that have past since a set time. Then you can do a proper comparison ....I included a sample that shows how to do it. The code is on the exit event of the Time Check field.

Hope that helps

Paul

Avatar

Former Community Member

Look at the code on the exit event of the Time Check field ......that shows the conversion of time to a number and then the if statements that are used to

determine which zone the time belongs to.

paul

Avatar

Level 2

Hi Paul,

thank you so much for your help. but i am doing something wrong.  it works on your form, but I cannot get it to work on mine.  I changed factor to my field name and am I supposed to change anything else?

sorry for the trouble.

sara

Avatar

Former Community Member

What format are you reporting the time in HH:MM:SS?

The function expects the time in a specific format. Are you getting any errors?

Paul

Avatar

Level 2

no errors. from patterns it says hh:MM:SS A.  I would upload the form so you could take a look at it but I can seem to figure out how to do that??

Avatar

Former Community Member

Send it to LiveCycle8@gmail.com but I will not be able to look at it until tomorrow.

Paul

Avatar

Level 2

I got it!!!!!!!!!!!!!! took a lot of work but this is how i got the following

12:01 am - 6:00 am = 1

6:01 am - 12:00 pm =.75

12:01 pm - 6:00 pm = .50

6:01 pm - 12:00 am = .25

Thank you Paul for your formula.

if ((Time2Num(this.formattedValue) >= 36060001) & (Time2Num(this.formattedValue) <= 57600001)) then

Factor.rawValue = 1

endif

if ((Time2Num(this.formattedValue) >= 57660001) & (Time2Num(this.formattedValue) <= 79200001)) then

Factor.rawValue = .75

endif

if ((Time2Num(this.formattedValue) >= 79260001) & (Time2Num(this.formattedValue) <= 100800001)) then

Factor.rawValue = .5

endif

if ((Time2Num(this.formattedValue) >= 100860001) & (Time2Num(this.formattedValue) <= 122340001)) then

Factor.rawValue = .25

endif