How To Check Difference Between Two Dates Using Client Script

Sagar_pawar
Tera Contributor

Hello all,

I want to find the difference between the incident creation date and the updation date. I am getting proper output using glideajax but now I want to achieve the same functionality using g_scratchpad. I have written code for that but am not getting proper output using g_scratchpad..

can anyone please guide me on this?

Thank You.

*-----------------------g_scratchpad code-----------------------*

Used Display BR

// var gd1=new GlideDateTime();
// gd1.setDisplayValue(current.sys_created_on);
// var gd2=new GlideDateTime();
// gd2.setDisplayValue(current.sys_updated_on);

// var duration=GlideDateTime.subtract(gd1,gd2);

g_scratchpad.d=duration.getDisplayValue();

4 REPLIES 4

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi,

Along with a display BR you have to write a client script to access the g_scratchpad variable at client side.

 

 


Thanks and Regards,

Saurabh Gupta

hello i tried with onload client  script but not getting output

 

Hello Abhi ,

I have Included Sample script which executes at Client Script for date difference 

Date Range Comparison.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	//Date to be between Jan 15th and Feb 15th only.
	//assuming date format of SN is dd/mm/yyyy. so create 2 dates in that format.
	var start_dateStr = formatDate(new Date(getDateFromFormat("15/01/2020 00:00:00",g_user_date_time_format)),g_user_date_time_format);
	var start_dateNum = getDateFromFormat(start_dateStr,g_user_date_time_format);
	var end_dateStr = formatDate(new Date(getDateFromFormat("15/02/2020 00:00:00",g_user_date_time_format)),g_user_date_time_format);
	var end_dateNum = getDateFromFormat(end_dateStr,g_user_date_time_format);
	var selected_dateNum = getDateFromFormat(newValue,g_user_date_time_format);
	if(selected_dateNum < start_dateNum || selected_dateNum > end_dateNum) {
		g_form.showFieldMsg('expected_start','Date should be between Jan 15th 2020 and Feb 15th 2020','error',true);
	} else {
		g_form.hideFieldMsg('expected_start',true);
	}
}
Please mark this as helpful and accept as a solution if this resolves your Ask.
Regards,

Shyamkumar

Kartik Choudha1
Tera Guru

Hey, 

 

As I see your pasted code, you have commented the first four script statements and then directly assigning the value of 'duration' variable. May be that's the case.

 

Below is the same logic and screenshot which I have implemented it's working fine.

KartikChoudha1_0-1673184088584.png

 

Client script

KartikChoudha1_1-1673184139632.png

 

Incident-

KartikChoudha1_2-1673184170650.png

 

KartikChoudha1_3-1673184195598.png

 

Hope this helps!

Regards,

Kartik Choudhary