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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Amrita,

Any update on this?

Regards

Ankur

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

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!