Restrict past date for DATE field only

AmritaT
Tera Expert

I'm comparing date field on my custom app and using the following script -

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

//Type appropriate comment here, and begin script below
//get the entered date string
var dtstring = g_form.getValue('u_date_begin');
//convert the string to a JavaScript Date/Time
var dt = new Date(dtstring);
//get the current date/time
var rightNow = new Date().valueOf();
//convert entered date to milliseconds
var dateToCheck = new Date(dtstring).valueOf();
alert("TODAY - " +rightNow);
alert("FIELD - " +dateToCheck);
//Compare the two numbers
if (dateToCheck < rightNow) {
alert('You cannot select a date in the past.');
g_form.setValue('u_date_begin','');
}
}

------------------

It works for the future dates. However, it gives me the error message if I add today's date,

My requirement is to compare only the date part of it and not the time. Any thoughts??

Thanks in advance!

 

 

 

1 ACCEPTED SOLUTION

AmritaT
Tera Expert

Ankur - I'm using the client script given below for this requirement :

 

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

var EventDateTime = newValue;
var dt = getDateFromFormat(EventDateTime, g_user_date_format);
var newdt = new Date(dt);
var EventDate = formatDate(newdt, 'yyyyMMdd');
var RightNow = new Date();
var RightNowDate = formatDate(RightNow, 'yyyyMMdd');

if (EventDate < RightNowDate) {
g_form.clearValue('u_date_expire');
g_form.showFieldMsg('u_date_expire', 'The Contract Date Expire must be after the current date', 'error', true);

}
}

 

This script is working well for me. Thanks!

View solution in original post

6 REPLIES 6

Allen Andreas
Administrator
Administrator

Hi,

Please see this thread for a possible solution. You need to get their displayValue.

https://community.servicenow.com/community?id=community_question&sys_id=624f3e69db58dbc01dcaf3231f96...

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Geetanjali Khy2
Mega Guru

Hi Amrit,

You can check using 'onChange' Client Script, that calls a script include to validate past date.

onChange() Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('DateandTIme');
ga.addParam('sysparm_name', 'getcurrentdate');
ga.addParam('sysparm_Publised', newValue);
ga.getXML(UpdatePublished);
}

function UpdatePublished(response, newValue){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer){
g_form.clearValue('published');
return;
}
}

 

Script Include:

var DateandTIme = Class.create();
DateandTIme.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getcurrentdate : function(){
var publisheddate = this.getParameter('sysparm_Publised');
if(publisheddate < gs.nowDateTime()){
return true;
}
else
return false;
},
type: 'DateandTIme'
});

 

Please check if it is helpful.

 

Thanks Geetanjali. This script doesn't work for scoped app. 😞

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Amruta,

First of all use display business rule where you can use g_scratchpad to store now date time since you cannot get that in client script. But this now Date time will be storing 1 value at a time when script runs and every time when value is changed it won't change the now date time. this is the limitation. you can go with GlideAjax also

Display BR Script:

g_scratchpad.dateTime=gs.nowDateTime();

Is your client script on that Date Begin field? I believe so; so you can use newValue there

Client Script

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

var nowDateTime = g_scratchpad.dateTime;

var dateFormat = g_user_date_time_format;

var dateOneFormat = getDateFromFormat(newValue, g_user_date_time_format);

var dateTwoFormat = getDateFromFormat(nowDateTime , g_user_date_time_format);

var difference=(dateOneFormat - dateTwoFormat );

if(difference < 0){

// it means it is past the difference is in milliseconds

alert("Past Date/Time not allowed");

}

}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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