How to write a catalog client script to compare minutes difference between 2 datetime fields?

Aditya Sinha
Tera Contributor

Hey everyone! 

 

I have a catalog item with 2 datetime fields (Let's call them S and E). I have to fulfill to conditions. 

 

1. Value entered in S  has to be atleast 15 minutes ahead of current time.

2. Value entered in E has to be atleast 30 minutes ahead of S.

 

Can you please help me with the code of it? 

 

For condition -1 

I wrote an onChange script 

 

var gr=new GlideDateTime();

var gb= GlideTime();

gb.setValue('00:00:900'); //for 15 minutes 

var a=gr.add(gb);

var b=g_form.getValue('S');

if (b<a);

{

g_form.clearValue('b');

}

This script isn't working. 

 

Your help would be appreciated

1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

Your onChange Catalog Client Script when S (named v_start_date in this example) changes should look more like this:

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

	var currentDateObj = new Date();
	var currentDateStr = formatDate(currentDateObj, g_user_date_format);
	var currentDate = getDateFromFormat(currentDateStr, g_user_date_format);
	var startDate = getDateFromFormat(g_form.getValue('v_start_date'), g_user_date_time_format);

	//prevent start date less than 15 minutes from now
	if (startDate - currentDate < 900000) {
		alert('Start Date must be at least 15 minutes from now.');
		g_form.clearValue('v_start_date');
	}
}

And the onChange Catalog Client Script when E (named v_end_date in this example) changes should look more like this:

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

	var endDate = getDateFromFormat(g_form.getValue('v_end_date'), g_user_date_time_format);
	var startDate = getDateFromFormat(g_form.getValue('v_start_date'), g_user_date_time_format);

	//prevent end date less than 30 minutes ahead of start date
	if (endDate - startDate < 1800000) {
		alert('End Date must be at least 30 minutes from Start Date.');
		g_form.clearValue('v_end_date');
	}
}