Expand my Community achievements bar.

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

Test for Day of Week using javascript?

Avatar

Level 2

Is it possible to test for what day of the week was selected in a Date field using javascript?

I have a script that checks if a "Day" radio button is checked and if so throws an error if the time entered is not between 6:00am and 3:30pm. I need to change the hours to between 6:00am and 2:30pm but only if the day of the week is a Friday (selected in the "Date" field).

Any ideas would really be appreciated.

if

(TimeReceived.isNull == true)

{

     xfa.host.messageBox("Please enter a Time Received");

     TimeReceived.rawValue

= null;

     xfa.host.setFocus("TimeReceived");

}

  else if ((TimeofCall.Day.rawValue ==0))

{

          if (TimeReceived.rawValue.search(":") > 0)

                    val1 = TimeReceived.rawValue.replace(":", "");

          else

                    val1 = TimeReceived.rawValue;

          if (val1 < 600 || val1 > 1530)

          {

               xfa.host.messageBox("If 'Time of Call' has Day selected, then the 'Time Received' must be within normal working hours (06:00 to 15:30).Make sure you are using military time, please re-enter.");

               TimeReceived.rawValue

= null;

               xfa.host.setFocus("TimeReceived");    
     }
}

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

FormCalc has a date function to get the day of the week.

// form1.page1.subform1.dayOfWeek::calculate - (FormCalc, client)

var date_ = Date2Num(date.formattedValue,"MM/DD/YYYY")
$ = Num2Date(date_,"EEE")

The pattern "EEE" returns the Mon, Tue, Wed, etcetera. You have to synch the date pattern on the DateTimeField ("date" in my case) with the script.

"dayOfTheWeek" could be a hidden field and you could reference the "dayOfTheWeek.rawValue" from your JavaScript.

Steve

View solution in original post

9 Replies

Avatar

Correct answer by
Former Community Member

FormCalc has a date function to get the day of the week.

// form1.page1.subform1.dayOfWeek::calculate - (FormCalc, client)

var date_ = Date2Num(date.formattedValue,"MM/DD/YYYY")
$ = Num2Date(date_,"EEE")

The pattern "EEE" returns the Mon, Tue, Wed, etcetera. You have to synch the date pattern on the DateTimeField ("date" in my case) with the script.

"dayOfTheWeek" could be a hidden field and you could reference the "dayOfTheWeek.rawValue" from your JavaScript.

Steve

Avatar

Level 2

Steve,

This sounds like exactly what I'm looking for, I'm just struggling a bit to get it working.

Does the "dayOfTheWeek" hidden field (that does the FormCalc) need to be a particular type of field? (Would a text field work?)

You mentioned the need to sync the date pattern on the DateTimeField with the script. Which pattern applies? There is Display, Edit, Validation and Data.

If the "dayOfTheWeek" field is visible for testing purposes, is there a way I can see that the EEE pattern is correctly returning the Day of the Week?

Thanks for helping the noobie on this one.

Avatar

Former Community Member

I am really getting frustrated with the new forum software. They go to all the effort to enable attachments and it doesn't always work. We would be better off not having it all. Sorry. I digress.

Does the "dayOfTheWeek" hidden field (that does the FormCalc) need to be a particular type of field? (Would a text field work?)

In my sample "dayOfTheWeek" is a textt field and it is not hidden so you can validate the results.

You mentioned the need to sync the date pattern on the DateTimeField with the script. Which pattern applies? There is Display, Edit, Validation and Data.

Display and Validation are defined as "date{MM/DD/YYYY}".

If the "dayOfTheWeek" field is visible for testing purposes, is there a way I can see that the EEE pattern is correctly returning the Day of the Week?

As stated above. Note, you can also use "E" pattern to return an integer (1-7) where 7 is Sunday, if you prefer.

Steve

Avatar

Level 2

Steve,

I did notice that your original post has an attachment, but it says "QUEUED" and I have not been able to download it.

I was able to get things working perfectly however with the additional info you provided in your last post.

Thanks so much, I really appreciate the help on this!

- Kevin

Avatar

Level 2

Steve,

Can I ensure that the user chooses a certain day (e.g. SUN).

I want the field to be invisible and yet that an error message will appear.

Thanks,

Yaniv

Avatar

Former Community Member

You could change the PDF, attached to this thread, as follows:

1) change the presence of 'dayOfTheWeek' to hidden

2) add the script, below, to the validate event of 'dayOfTheWeek'

// form1.page1.subform1.dayOfWeek::validate - (JavaScript, client)

if (this.rawValue != "Sun") {

     xfa.host.messageBox("The selected date is not a Sunday.");

}

Steve

Avatar

Level 2

Thank you so much Steve. I have changed the 'dayOfTheWeek' to hidden and added your script to the validate event of 'dayOfTheWeek' and it works.

The error message, though, appears as soon as the user opens the document. Is there a way to make this error message pop up only after the user has attempted to enter a correct date?

Yaniv

Avatar

Former Community Member

Whoops. Try this instead. It will test the presence of a value before testing the day.

// form1.page1.subform1.dayOfWeek::validate - (JavaScript, client)

if (!(this.isNull || this.rawValue.length == 0)) {

     if (this.rawValue != "Sun") {

          xfa.host.messageBox("The selected date is not a Sunday.");

     }

}

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] ----