Pull only the current time with Client Script

TacoJoe
Tera Contributor

Hello all,

 

I am working on configuring a record producer that will need to display certain variables depending on if it is during business hours or after hours. My thought was to use a client script to get the current time and hide/show variables depending on if its before or after the set time. I am able to get date and time, but wanted to know if there is a way to get only the current time to be captured. 

 

Thanks for any assistance/guidance. 

 

Joe

3 REPLIES 3

Ankur Bawiskar
Tera Patron

@TacoJoe 

what type of variable is this?

you want current time in local time i.e. timezone of the logged in user or GMT time?

share some details and screenshots

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

VaishnaviK16371
Tera Contributor

Hi @TacoJoe !!

Yes, you can get only the current time using standard client-side JavaScript.

Example in a Record Producer Catalog Client Script:

var now = new Date();
var hours = now.getHours(); // 0–23
var minutes = now.getMinutes(); // 0–59

You can then use this to show/hide variables:

if (hours >= 8 && hours < 17) {
g_form.setDisplay('after_hours_variable', false);
} else {
g_form.setDisplay('after_hours_variable', true);
}

Note: This uses the end user's browser time. If you need true business-hours logic (holidays, time zones, daylight savings), best practice is to use a GlideSchedule in a client-callable Script Include and call it via GlideAjax.

 

Mark this as Helpful if it clarifies the issue.
Accept the solution if this answers your question.

Regards,
Vaishnavi
Associate Technical Consultant

ManishSharma2
ServiceNow Employee

Hi @TacoJoe ,

 

My recommendation will be to implement this via a combination of an onLoad Catalog client script and a GlideAjax.

Keep the "business hour" validation to server side using GlideDateTime APIs and just return true/false to the client side via GlideAjax.

Leverage this retured value to show or hide the variables/fields on the Record producer using g_form.setDisplay("field_name").