How to restrict Start date time should not be more than 30 minutes from current time ?

ramuv
Tera Expert

How to restrict Start date time should not be more than 30 minutes from current time ?

Scenario:

1. Added two fields on catalog item , field1 : Start Date time , Field2 : End Date time

 2 .Start date should not be more than 30 minutes from current time .

3.  There should not be less than  difference of 60 minutes between start date and end date .

4 REPLIES 4

JohnnySnow
Kilo Sage

Can you please share the code that you have tried ?

You'll have to use Date functions and client scripts to achieve this.

Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.

Deepak Shaerma
Kilo Sage

Hi @ramuv 

#### Step 1: Create the Catalog Item
- Navigate to Maintain Items and create a new catalog item or open an existing one.
- Add the two fields for “Start Date time” and “End Date time” to the catalog item.

#### Step 2: Add Client Scripts
- Navigate to Client Scripts and create a new Client Script for your catalog item.

### Client Script for Start Date Validation

Name: Validate Start Date Time
Table: Your catalog item’s table (e.g., sc_cat_item)
Type: onChange
Field name: Variable containing the “Start Date time” field

Script:


function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === ‘’) {
return;
}

// Restrict Start Date Time to not be more than 30 minutes from current time
var currentDateTime = new GlideDateTime();
var startDateTime = new GlideDateTime(newValue); // The new start date value

// Calculate difference in minutes
var diffMinutes = (startDateTime.getNumericValue() - currentDateTime.getNumericValue()) / 1000 / 60;

if (diffMinutes > 30) {
g_form.showFieldMsg(control.getName(), ‘Start Date/Time cannot be more than 30 minutes from the current time.’, ‘error’);
g_form.clearValue(control.getName());
}
}



### Client Script for End Date Validation

Name: Validate End Date Time
Table: Your catalog item’s table (e.g., sc_cat_item)
Type: onChange
Field name: Variable containing the “End Date time” field

Script:


function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === ‘’) {
return;
}

// Ensure there is at least a 60-minute difference between Start Date Time and End Date Time
var startDateTime = g_form.getValue(“start_date_time_variable_name”); // Replace with your variable name
var endDateTime = new GlideDateTime(newValue);

if (startDateTime !== ‘’) {
var startDateTimeObj = new GlideDateTime(startDateTime);

// Calculate difference in minutes
var diffMinutes = (endDateTime.getNumericValue() - startDateTimeObj.getNumericValue()) / 1000 / 60;

if (diffMinutes < 60) {
g_form.showFieldMsg(control.getName(), ‘End Date/Time must be at least 60 minutes later than Start Date/Time.’, ‘error’);
g_form.clearValue(control.getName());
}
}
}

 Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards
Deepak Sharma 

@Deepak Shaerma 

Your script will not work as you are using server side class in client side.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@ramuv 

you need to have 2 onChange client scripts

1) start date - use GlideAjax and check if it's within 30mins from current time

2) end date - use GlideAjax and pass start date and validate diff between both dates is more than 60mins or not

what did you start with and where are you stuck?

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