Two conditions in which I want to run the same code

Dec 16, 2019

Copy link to clipboard

Copied

Hey everyone, 

 

I have a script which does some stuff with time ( HH:MM format) where its important to check that the entered start time is before the end time. (because.. you know, time)

 

This is the jest of what I have;

if (hour start > hour end)){
            // ERROR: start later than end
        }else if(hours are equal)){
            // check if minutes are entered correctly
            if (minutes start>minutes end)){
                // ERROR: start later than end
            }else{
                // Time was entered correctly, start
            
        }}else{
            // Time was entered correctly, start
}

 

So basically I have two conditions in which I want to run the same code; I reckon I can solve this by using a state variable but was wondering if there was perhaps another way (i.e. a goto line or perhaps a function in a function, is there such a thing?) I got the feeling those might be unnecisarily complicated so perhaps theres an easyer way to do this. (I've done some attempts at using the date.objects but couldnt get the util.scand to work with the HH:MM format

 

Any suggestions?

 

Thank you in advance for any trouble to be taken

Then do this:

 

((hours start > hours end) OR ((hours start == hours end) AND (minutes end > minutes start)))

TOPICS
Acrobat SDK and JavaScript

Views

124

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

Two conditions in which I want to run the same code

Dec 16, 2019

Copy link to clipboard

Copied

Hey everyone, 

 

I have a script which does some stuff with time ( HH:MM format) where its important to check that the entered start time is before the end time. (because.. you know, time)

 

This is the jest of what I have;

if (hour start > hour end)){
            // ERROR: start later than end
        }else if(hours are equal)){
            // check if minutes are entered correctly
            if (minutes start>minutes end)){
                // ERROR: start later than end
            }else{
                // Time was entered correctly, start
            
        }}else{
            // Time was entered correctly, start
}

 

So basically I have two conditions in which I want to run the same code; I reckon I can solve this by using a state variable but was wondering if there was perhaps another way (i.e. a goto line or perhaps a function in a function, is there such a thing?) I got the feeling those might be unnecisarily complicated so perhaps theres an easyer way to do this. (I've done some attempts at using the date.objects but couldnt get the util.scand to work with the HH:MM format

 

Any suggestions?

 

Thank you in advance for any trouble to be taken

Then do this:

 

((hours start > hours end) OR ((hours start == hours end) AND (minutes end > minutes start)))

TOPICS
Acrobat SDK and JavaScript

Views

125

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
Dec 16, 2019 0
Dec 16, 2019

Copy link to clipboard

Copied

More information is needed before we can provide a suggestion.

Are the start and end times in form fields?

What are the two conditions?

Why can't the same code be used in both places?

 

 

 

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
Reply
Loading...
Dec 16, 2019 0
Dec 17, 2019

Copy link to clipboard

Copied

Good morning Thom, (here atleast 😉 )

 

I'd prefer using the actual date datatype (even though input of date + time is somewhat lacking) however I didn't get this to work. If someone can provide a working code for a situation where field A,B using layout "d/m/yyyy HH:MM" is used to calculate the number of hours between date A & date B then that would be extraordinary.

 

As to your request for further information;

 

-Are the start and end times in form fields?

Yes they are, input is the HH:MM format  , 

 

I split the string on the : and then parse both as integers, the left side being hours and the right side as minutes. It gets the job done. 

 

-What are the two conditions? 

I'm not sure I follow, do you mean the booleans? The variables in my example are integers depicting hours and one depicting minuts, if the hours are the same I want to check if the start time minutes are closer than the end time minutes (as the hours are equal it results that the start time is after the end)

 

-Why can't the same code be used in both places?

It can be,  my question is mostly about best practise (and possibly about the date data type)

 

 

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
Reply
Loading...
Dec 17, 2019 0
Dec 17, 2019

Copy link to clipboard

Copied

Basically, you just need to use an OR operator to do it, like this:

 

if (condition1 || condition2) {

// do something
}

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
Reply
Loading...
Dec 17, 2019 0
Dec 17, 2019

Copy link to clipboard

Copied

The problem is the minutes are only of import when the hours are equal. 

 

If I do ( hours start > hours end OR minutes end higher than start) then id get a wrong boolean in most workable cases where start time 8:10 , end time 9:00 would trigger the fail boolean. (nb. 10 higher than 00 )

 

somewhere deep down I think the most eloquent solution is using the date type, a script that calculates the number of hours between i.e. Field A: 15-12-19 12:00    & Field B:  17-12-19 10:00    would be glorious (limitation of my current setup is its per single day only)

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
Reply
Loading...
Dec 17, 2019 0
Dec 17, 2019

Copy link to clipboard

Copied

Then do this:

 

((hours start > hours end) OR ((hours start == hours end) AND (minutes end > minutes start)))

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
Reply
Loading...
Dec 17, 2019 1
Dec 17, 2019

Copy link to clipboard

Copied

Yeah thatd solve it too, thanks!

 

I think the actual variables being arrays made me overcomplicate it somehow, lesson learned.

 

It still seems like a lot of work for something that has a built in datatype however, if someone has any ideas how to get it to work that way thatd be great.

 

For now ill mark this as the answer though, thanks! 

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
Reply
Loading...
Dec 17, 2019 0