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

Chilled. 🙂

I wanted to say Date() chill a bit 🙂 

OK, that is different, but still not correct. 😁
The correct answers is in the post linked by @Dhananjay Pawar: using function getDateFromFormat and "constant" g_user_date_time_format.

You see a user can select date/time formats for themselves that cannot be interpret by Date, and thus it will lead to intermittent functioning.

E.g.

var d = new Date('10.04.2024 13:30:33')
console.log(d);

prints

Fri Oct 04 2024 13:30:33 GMT+0300

but if the current user has selected for himself this as the German date/time format then it should be interpreted as

Wed Apr 10 2024 13:30:33 GMT+0300

-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