The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Client Script - Start Date + 24 hours

Jeff Ward
Tera Contributor

Hi!

I'm trying to write a simple Client Script that would compare Planned Start Date and Planned End Date for Change Requests to ensure end dates are kept within 24 hours of the start date.

 

I've tried the below script with and without the parseInt() lines and neither seems to be working correctly. I presume I need to convert the original start_date and end_date values to seconds in order to add 86400 (24 hours in seconds), but I haven't found anything that works with Client Scripts yet.

 

Is there a simple way to do this, or can this only be done via Inline Scripts?

 

Thanks!

 

 

function onSubmit() {
    var start_date = g_form.getValue('start_date');
	var start_date_int = parseInt(start_date);
    var end_date = g_form.getValue('end_date');
	var end_date_int = parseInt(end_date);
	var add_time = 60*60*24;
	var new_end_date = start_date_int + add_time;
    if (end_date_int > new_end_date) {
        alert("End Date must be within 24 hours of Start Date ");
        return false;
    }
}

 

2 ACCEPTED SOLUTIONS

Musab Rasheed
Tera Sage
Tera Sage

Hello,

You can easily do with this UI policy.

MusabRasheed_0-1670311951420.png

 

MusabRasheed_1-1670311982533.png

Please hit like and mark my response as correct if that helps
Regards,
Musab

View solution in original post

jaheerhattiwale
Mega Sage
Mega Sage

@Jeff Ward Tried and Tested solution. Please use below code

 

var D_one = g_form.getValue("end_date");
var D_two = g_form.getValue("start_date");

var dateOneFormat = getDateFromFormat(D_one, g_user_date_time_format);
var dateTwoFormat = getDateFromFormat(D_two, g_user_date_time_format);

var difference=(dateOneFormat - dateTwoFormat )/(1000 * 60 * 60);

if(Number(difference) > 24){
    alert("End Date must be within 24 hours of Start Date ");
    return false;
}
 
Please mark as correct answer if this solves your issue.
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

5 REPLIES 5

Musab Rasheed
Tera Sage
Tera Sage

Hello,

You can easily do with this UI policy.

MusabRasheed_0-1670311951420.png

 

MusabRasheed_1-1670311982533.png

Please hit like and mark my response as correct if that helps
Regards,
Musab

Can the UI Policy condition factor in 24 BUSINESS hours instead?

Thanks,

Ron Hoyt.

jaheerhattiwale
Mega Sage
Mega Sage

@Jeff Ward Tried and Tested solution. Please use below code

 

var D_one = g_form.getValue("end_date");
var D_two = g_form.getValue("start_date");

var dateOneFormat = getDateFromFormat(D_one, g_user_date_time_format);
var dateTwoFormat = getDateFromFormat(D_two, g_user_date_time_format);

var difference=(dateOneFormat - dateTwoFormat )/(1000 * 60 * 60);

if(Number(difference) > 24){
    alert("End Date must be within 24 hours of Start Date ");
    return false;
}
 
Please mark as correct answer if this solves your issue.
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Jeff Ward
Tera Contributor

Perfect! Thank you both for the replies. This is now working as expected.

 

Take care!