Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

End Date and Time can't be before Start end date and time

Jen11
Tera Expert

I have two date and time fields, start and end. I would like end date/time field not be before start date/time field.  Can someone please help with an onChange client script as i am struggling.

Thank you,

4 REPLIES 4

Mohith Devatte
Tera Sage
Tera Sage

Hello Jen you can do this in client script 

g_form.hideFieldMsg('end_date');



var dtStartForm = g_form.getValue('start_date');



var dtStart = getDateFromFormat(dtStartForm,g_user_date_time_format);



if (dtStartForm != '') {



       var dtEndForm = g_form.getValue('end_date');



       var dtEnd = getDateFromFormat(dtEndForm, g_user_date_time_format);



       //g_form.addInfoMessage('Start Date : ' + dtStart + ' : End Date : ' + dtEnd);



       if (dtEnd < dtStart) {



               g_form.showFieldMsg('end_date', 'Planned end date cannot be before start date', 'error');



       }



}


Hope this helps 

MARK MY ANSWER CORRECT IF THIS HELPS YOU

Thank you for your help, i did the following and its not working:

 

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

   //Type appropriate comment here, and begin script below
    
    
    var dateFormat=g_user_date_time_format;


var D_one = g_form.getValue("u_date_time_first_reported");


var D_two = g_form.getValue("u_date_time_resolved");


var dateOneFormat = getDateFromFormat(D_one , g_user_date_time_format);


var dateTwoFormat = getDateFromFormat(D_two , g_user_date_time_format);


if(dateTwoFormat >dateOneFormat )
{
alert('End date cant be less than start date');
}
    
    
    
    
   
}

hello jen my bad gave the wring script i think its better to use the glide ajax 

can you use this script by creating a script include

refer this below link and check for saprem.d answer for the script 

https://community.servicenow.com/community?id=community_question&sys_id=56c70fe9db1cdbc01dcaf3231f96...

Hope this helps

Mark my answer correct if it helps you

 

Mohith Devatte
Tera Sage
Tera Sage

Hello jen  i think its better to use the glide ajax 

can you use this script by creating a script include

refer this below link and check for saprem.d answer for the script 

https://community.servicenow.com/community?id=community_question&sys_id=56c70fe9db1cdbc01dcaf3231f96...

Hope this helps

Mark my answer correct if it helps you