Compare end date with start date

Vijay Baokar
Kilo Sage

Hello Everyone,

 

I have 2 fields Start and End. Whenever i select any furfure date in start date field , my end date should not be more than 21 days from start date. below is the script i tried with but didn't work:

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   var strtdate = g_form.getValue('start');
   var endate = g_form.getValue('end');
   if(strtdate && endate)
   {
var strdatetime = new GlideDateTime(strtdate);
var endatetime = new GlideDateTime(endate);

var daysdiff = GlideDateTime.substract(endatetime, strdatetime);
var days = daysdiff.getValue()/1000 /60 / 60 / 24;

if(days > 21)
 
{
    alert('End date can not be more than 21 days from start date.');
    g_form.clearValue('end');
}

   }

 
   
}
2 ACCEPTED SOLUTIONS

kkrushkov
Mega Sage

Hi, @Vijay Baokar 

Try using the Date() object;

 

 

 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }   
var strtdate = g_form.getValue('start');
var endate = g_form.getValue('end');

if(strtdate && endate){

var strdatetime = new Date(strtdate);
var endatetime = new Date(endate);
var date1_ms = strdatetime.getTime();
var date2_ms = endatetime.getTime();

var msdiff = date2_ms - date1_ms;
var daysdiff  = Math.floor(msdiff  / (1000 * 60 * 60 * 24));

if(daysdiff > 21)
{
    alert('End date can not be more than 21 days from start date.');
    g_form.clearValue('end');
}
}
}

 

 

 

View solution in original post

-O-
Kilo Patron
Kilo Patron

When speaking of web applications, there's a client side and a server side.

One set of APIs is available client side and another set of APIs is available server side.

Scripts running client side cannot directly call APIs that exist only server side.

That is what you have here: an onChange Client Script running client side, trying to call an API - GlideDateTime that is only available server side.

So you need to either only use APIs that are available client side - like the community post shared by @Dhananjay Pawar explains, or you need to do a GlideAjax call to "reach" server side APIs - which is a wrapper around the standard XMLHttpRequest.

There are other client side APIs in other UI paradigms, like Portal, or Next Experience - that help communication with server side - just FYI.

 

When not sure which API is available in one of the two environments (client side vs. server side), you should go to https://developer.servicenow.com/dev.do and activate the Reference -> APIs -> Client (for available client side APIs) or Reference -> APIs -> Server Scoped or Reference -> APIs -> Server Global (for available server side APIs).

18.png

 

View solution in original post

8 REPLIES 8

Dhananjay Pawar
Kilo Sage

Hi,

Refer this article it will help you to resolve your issue.

https://www.servicenow.com/community/developer-articles/date-validations-client-scripts/ta-p/2298860...

 

Thanks.

 

kkrushkov
Mega Sage

Hi, @Vijay Baokar 

Try using the Date() object;

 

 

 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }   
var strtdate = g_form.getValue('start');
var endate = g_form.getValue('end');

if(strtdate && endate){

var strdatetime = new Date(strtdate);
var endatetime = new Date(endate);
var date1_ms = strdatetime.getTime();
var date2_ms = endatetime.getTime();

var msdiff = date2_ms - date1_ms;
var daysdiff  = Math.floor(msdiff  / (1000 * 60 * 60 * 24));

if(daysdiff > 21)
{
    alert('End date can not be more than 21 days from start date.');
    g_form.clearValue('end');
}
}
}

 

 

 

@kkrushkov it didn't work.

Note: i have both the fields as date time

@Vijay Baokar I updated my answer. Check it now