Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

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

suvro
Mega Sage

Hi YOu can go through below article

https://community.servicenow.com/community?id=community_question&sys_id=38c40be9dbd8dbc01dcaf3231f9619e3

Raghu Ram Y
Kilo Sage

Hi,

You need to convert date format as you like..

try the below code..

var date = '' //pass your date here
var simpleDateFormat = 'YYYY MM DD HH:mm:ss'; // Simple Date Time format
var gdt = new GlideDateTime(); 
gdt.setDisplayValue(date.simpleDateFormat); //Set time 
gs.addInfoMessage(gdt.getDisplayValue()); // Output time 

Thanks that helped a bit, the time is now setting properly but the date is still the wrong way round. i think i need to reformat the date before i pass it into a GlideDateTime variable because once it's in it gets converted and there's no turning back.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

You can simply validate if selected date/time is in future or if the end time is before the start time without Ajax

Any specific reason you wish to use script include?

Regards
Ankur

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