Inconsistency from GlideDateTime in GlideAjax

Dubz
Mega Sage

Hi All,

So i'm using GlideAjax to do some date/time validation in an onChange client script. I basically want to check if the time provided is in the future or if the end time is before the start time. I'm sending back the value of date/time fields and using those to poulate GlideDateTime variables.

the issue i'm seeing is that, if i send a value of a date that is from the 13th onwards, the GlideDateTime variable correctly populates the date but zero's out the time to 00:00:00. If i send a value of a date that is from the 1st to the 12th the GlideDateTime variable switches the day and month around but now returns the correct time.

Example:

value sent: '15/06/2022 10:42:49' GlideDateTime variable: '2022-06-15 00:00:00'

value sent: '07/06/2022 10:53:44' GlideDateTime variable: '2022-07-06 10:53:44'

How can i get my GlideDateTime variables to conform to the instance date/time format? I've had a look through the reference guide but it's typically opaque and the various methods i've used have just thrown errors.

Cheers

Dave

1 ACCEPTED SOLUTION

Hi,

for validation of date in future check this

you can use UI policy on that field for this and no need of client script

Start Date [Before] Today

Script as below in Execute if true section

function onCondition() {

    alert('Should not be select the Past Date');
    g_form.clearValue('start_date');

    g_form.setMandatory('start_date', true); // if it is mandatory

}

 

 

Sharing link for help for UI policy for Start and End

Date and time validation/ start date and end date validation.

onSubmit script for same

function onSubmit() {
	//Type appropriate comment here, and begin script below

	g_form.hideErrorBox('start_date');
	g_form.hideErrorBox('end_date');

	if(g_form.getValue('start_date') != '' && g_form.getValue('end_date')){
		var start = new Date(g_form.getValue('start_date')).getTime();
		var end = new Date(g_form.getValue('end_date')).getTime();
		if(end < start){
			var message = 'Please give valid start and end dates';
			g_form.showErrorBox('start_date', message);
			g_form.showErrorBox('end_date', message);
			return false;
		}

	}
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

8 REPLIES 8

I didn't think i could do it in the client script itself, if you have a method please do share.

 

Hi,

for validation of date in future check this

you can use UI policy on that field for this and no need of client script

Start Date [Before] Today

Script as below in Execute if true section

function onCondition() {

    alert('Should not be select the Past Date');
    g_form.clearValue('start_date');

    g_form.setMandatory('start_date', true); // if it is mandatory

}

 

 

Sharing link for help for UI policy for Start and End

Date and time validation/ start date and end date validation.

onSubmit script for same

function onSubmit() {
	//Type appropriate comment here, and begin script below

	g_form.hideErrorBox('start_date');
	g_form.hideErrorBox('end_date');

	if(g_form.getValue('start_date') != '' && g_form.getValue('end_date')){
		var start = new Date(g_form.getValue('start_date')).getTime();
		var end = new Date(g_form.getValue('end_date')).getTime();
		if(end < start){
			var message = 'Please give valid start and end dates';
			g_form.showErrorBox('start_date', message);
			g_form.showErrorBox('end_date', message);
			return false;
		}

	}
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Actually just came across the full article Mark wrote on this, very helpful. Thanks for the assist.

Glad to help.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader