How to restrict start date to be selected as past date.

VIKAS MISHRA
Tera Contributor

I have created below mentioned onchange client script, this is working that  i am not able to select the past date but along with past date its not even allowing me to select the today's date also and giving me the same error. please suggest

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

  var date1 = new Date(g_form.getValue('start_date'));
var date2 = new Date();
if(date1 <date2){
alert("Add future day");
g_form.clearValue('start_date');
}
   
}

 

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@VIKAS MISHRA 

use this

if(date1 <= date2){

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

SourabhAkiwate
Tera Expert

Hello @VIKAS MISHRA ,

Please use the below OnChange Client script,

 

function onChange(control, oldValue, newValue, isLoading) {
 
    if (isLoading || newValue == '') {
        return;
    }
 
    var currentDate = new Date();
    var currentDateStr = formatDate(currentDate, g_user_date_format);
    var currentDate = getDateFromFormat(currentDateStr, g_user_date_format);
 
    var startDate = getDateFromFormat(newValue, g_user_date_format);
 
    if (startDate < currentDate) {
g_form.ClearValue('start_date')
        g_form.showErrorBox('start_date',getMessage("Start date should be today or future date."));
return;
        
    }
}
 
 
Please mark this "Helpful" if my solution is useful to you.
Thank you
Sourabh A

Robert H
Mega Sage

Hello @VIKAS MISHRA ,

 

Please use this script if you are using a Date (no time) field:

 

var startDate = getDateFromFormat(
		g_form.getValue('start_date'), g_user_date_format),
	today = new Date().setHours(0, 0, 0, 0);

if (startDate < today) {
	g_form.clearValue('start_date');
	g_form.showErrorBox('start_date', 'Start date cannot be in the past');
}

 

Use this if you are using a Date/Time field:

 

var startDate = getDateFromFormat(
		g_form.getValue('start_date'), g_user_date_time_format),
	now = new Date().getTime();

if (startDate < now) {
	g_form.clearValue('start_date');
	g_form.showErrorBox('start_date', 'Start date cannot be in the past');
}

 

Or create a UI Policy and UI Policy Action as shown below:

 

RobertH_0-1744695935528.png

RobertH_1-1744695969672.png

 

Regards,

Robert

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @VIKAS MISHRA 

Not sure what the exact case is here, but did you try using a UI Policy to restrict the user from selecting a past date? UI Policies are low-code and easy to configure.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************