Expand my Community achievements bar.

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

strange behaviour with time script

Avatar

Former Community Member

Hi all,

i'm having the strangest behaviour with one of my scripts, and i cannot figure out where the problem is.

Before showing the code, here's the principle.

My form has two time fields: "Injection Time" and "Post injection Time"

The script runs at the exit event of "injection time" and looks if the time of injection is before the "post injection time"

Here's my code:

//get the value of the field "Injection time"

var injection = this.formattedValue;

//create an new Date object
var injectionTime = new Date();

//split the string to get the HH MM and SS values of the formattedValue (HH:MM:SS)
var injectionTimeHoursString = injection.substr(0,2);
var injectionTimeMinutesString = injection.substr(3,2);
var injectionTimeSecondsString = injection.substr(6,2);

//parse the strings into integer

var injectionTimeHoursInt = parseInt(injectionTimeHoursString);
var injectionTimeMinutesInt = parseInt(injectionTimeMinutesString);
var injectionTimeSecondsInt = parseInt(injectionTimeSecondsString);

//set the integer values into my Date object
injectionTime.setHours(injectionTimeHoursInt, injectionTimeMinutesInt, injectionTimeSecondsInt,0);

//do the same for the other time field "Post injection time"
var post = post_injection.post_time.formattedValue;
var postTime = new Date();
var postTimeHoursString = post.substr(0,2);
var postTimeMinutesString = post.substr(3,2);
var postTimeSecondsString = post.substr(6,2);
var postTimeHoursInt = parseInt(postTimeHoursString);
var postTimeMinutesInt = parseInt(postTimeMinutesString);
var postTimeSecondsInt = parseInt(postTimeSecondsString);
postTime.setHours(postTimeHoursInt, postTimeMinutesInt, postTimeSecondsInt,0);


//calculate the difference between both
var diff = postTime.getTime() - injectionTime.getTime();

//if the difference gives a negative result, display a message, empty the fields and set focus
if(diff<0) {
     xfa.host.messageBox("The time of injection should be sooner than the time of post-injection activity measure.\n\nPlease re-enter correct times.","Incompatibility of times");
     this.rawValue = "";
     post_injection.post_time.rawValue = "";
     xfa.host.setFocus(post_injection.post_time);
}

The code seems to work quite well... except with some values!

if postTime is 12:00:00

and injetion is 12:04:00

I get the error message and everything, so that's OK

BUT

when postTime is 12:00:00

and injectionTime is 12:08:00

No message! as if the difference was not negative! (the difference is zero...)

when i display the times in a messagebox, 12:04:00 gives 12:04:00 GMT bla bla... but 12:08 gives 12:00:00 GMT etc...

same problem with 12:09, but not 12:10 for example...

Any thought??

Thank you.

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

Thank you, but since i am used to javascript, i'll stick to it.

I did figure out the problem. It's a javascript bug with the ParseInt method !!

check out there: http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256C85006A6604

parseInt("08") = 0 !!

how to waste 2 hours for a stupid bug...

View solution in original post

3 Replies

Avatar

Level 10

Hi,

Have you tried using FormCalc instead of JavaScript?

FormCalc comes with some very handy time (and date) functions. When using a time object, you can use Time2Num to convert the inputted time to a number from the epoch. Once you compare the two numbers you cn convert back to a time or take appropriate action.

Niall

Avatar

Correct answer by
Former Community Member

Thank you, but since i am used to javascript, i'll stick to it.

I did figure out the problem. It's a javascript bug with the ParseInt method !!

check out there: http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256C85006A6604

parseInt("08") = 0 !!

how to waste 2 hours for a stupid bug...

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----