Validate date and time on a due date field that the entered value is not in the Past

Supritha3
Tera Contributor

Hi Team,

 

I have a requirement to validate the entered date and Time is not in the past on the incident due date field.

I've tried to achieve this via Client Script and a Script Include.

 

The onChange Client script I have written on change of due date:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
 
var ga = new GlideAjax('DateandTime');
ga.addParam('sysparm_name', 'getcurrentdate');
ga.addParam('sysparm_dueDate', newValue);
ga.getXML(UpdatePublished);
}
 
function UpdatePublished(response) {
 var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.addInfoMessage('answer is : ' +answer);
 if (answer == "true") {
 
g_form.addErrorMessage('Please ensure that the Due date is in the future');
g_form.clearValue('due_date');
    return;
   }
else{
return;
}
 
}
 
Script Include:
 
var DateandTime = Class.create();
DateandTime.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getcurrentdate: function() {
var DueDate = this.getParameter('sysparm_dueDate');  
var dueDate1 = new GlideDateTime(DueDate);
var time1 = new GlideDateTime();
gs.addInfoMessage("due date formatted : " +dueDate1 +' current timeanddate: ' +time1 + "due date asis: "+" " +DueDate);
if(dueDate1 < time1){
return true;
}
else
return false;
},
 
    type: 'DateandTime'
});
 
 
while the above codes are working fine in validating the dates but are failing in validating the time.
 
Also using the below piece of code also gives time in zero's which is a wrong data.
 
var DueDate = this.getParameter('sysparm_dueDate');  
var dueDate1 = new GlideDateTime(DueDate);
 
Can someone please help me figure out the issue.
13 REPLIES 13

@Supritha3 

try this once

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

	if(g_form.getValue('due_date') != ''){
		var nowTime = new Date().getTime();
		var dueDate = new Date(getDateFromFormat(g_form.getValue('due_date'), g_user_date_time_format)).getTime();
		if(dueDate < nowTime){
			g_form.addErrorMessage('Please ensure that the Due date is in the future');
			g_form.clearValue('due_date');
			g_form.setMandatory('due_date');
		}
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi Ankur,

 

I've tried the above solution, it is validating if I'm entering the past time, however if I enter a time in future, say 2 hours from the current time but on the same date, it still throws up an error.

I have attached the screen shot below which gave me the info message.

Scenario 1: as expected generated error on entering past time

Scenario 2: Logs shown when entering future dates. 

what I observe from the both the scenarios is that the time remains same no matter what time is entered.

 

Supritha3_1-1692792738430.png

 

 

Hi Ankur,

 

I've tried the above solution, it is validating if I'm entering the past time, however if I enter a time in future, say 2 hours from the current time but on the same date, it still throws up an error.

I have attached the screen shot below which gave me the info message.

Scenario 1: as expected generated error on entering past time

Scenario 2: Logs shown when entering future dates. 

what I observe from the both the scenarios is that the time remains same no matter what time is entered.

 

Supritha3_0-1692791313415.png

 

@Supritha3 

 

Use gs.DateDiff();

You need to call script include in your client script with below code

var gdt1 = GlideDateTime();
var due_date = this.getParameter('sys_due_date');//this is due date field from client script
var duration1 = gs.dateDiff(gdt1.getDisplayValue(),due_date.getDisplayValue(), true);
return duration;//from script include
if(answer<=0){
g_form.addInfoMessage('Date should be future date');
}

 If gdt1>gdt2 -this condition becomes true when due date is past date((duration1 returns negative value))

gdt1==gdt2 -when both dates are same (duration1 returns value-0)

gdt1<gdt2-when your due date is future value(duration1 returns positive value)

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact
 
Thanks,
Manjusha Bangale