Unable to Select the Current date

msm4
Mega Guru

Hi Team,

Please find the below snippet i used to validate the date field to select only the current and future date.

But the problem is im unable to select the current date.

Please guide where i m wrong.

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading || newValue == '') {

          return;

    }

var today = new Date();

var dateMS = today.getTime();

var current_date = new Date();

current_date.setTime(dateMS);

var new_date = new Date(getDateFromFormat(newValue, g_user_date_time_format));

g_form.setValue('ftr_needed_by',formatDate(current_date,g_user_date_time_format));

if(new_date>=current_date)

{

// this is valid

}

  else{

  alert("selected date should be current date or future date");

  g_form.setValue('axa_asia_ph_var_date_needed','');

}

}

Thanks in Advance

1 ACCEPTED SOLUTION

Harneet Sital
Mega Sage
Mega Sage

Hi,



Rather than client script use the following UI policy with the following condition :



Capture.PNG



Write following script :


function onCondition() {


      g_form.setValue('date_of_joining', "'');


      alert("Enter valid date");


}



Its simple and works in all the cases.


View solution in original post

10 REPLIES 10

arnabwa
Giga Guru

Hi Smitha,



This OnChange client script would serve your purpose :



function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  return;


  }



var currentDate = new Date();


var currentDateFormat = formatDate(currentDate, g_user_date_time_format); // get the user date format


var currentDateFinal = getDateFromFormat(currentDateFormat, g_user_date_time_format);//convert to the user date format


//alert(currentDateFinal);




//get duration


var duration = g_form.getValue('u_startdt');//get your variable field date


var durationFinal = getDateFromFormat(duration, g_user_date_time_format);//convert to the user date format


//alert(durationFinal);




var difference = durationFinal - currentDateFinal;



if (difference < 0) {


  //flag++;


  alert('You cannot select a date in the past.');


  g_form.setValue('u_startdt', ''); //Replace u_startdt with your variable name


}



Please let   me know if you have further queries.



Thanks,


Arnab


msm4
Mega Guru

Hello Everyone,



I tried all the above scripts you provided .. but no luck.


It seems very simple but im unable to trigger-out the mistake.



function onChange(control, oldValue, newValue, isLoading) {


if (isLoading || newValue == '') {


          return;


    }


var today = new Date();



var dateMS = today.getTime();



var current_date = new Date();



current_date.setTime(dateMS);



var selected_date = new Date(getDateFromFormat(newValue, g_user_date_format));



g_form.setValue('ftr_needed_by',formatDate(current_date,g_user_date_format));




  alert(selected_date);


  alert(current_date);



if(selected_date >= current_date)


{


//valid condition


}


  else{


  alert("selected date should be current date or future date");


  g_form.setValue('axa_asia_ph_var_date_needed','');


}


}




Please can anyone correct the code above.


Geoffrey2
ServiceNow Employee
ServiceNow Employee

I found that getDateFromFormat function wasn't working for me.   Which is why my code above does not use it.


For what it's worth, my script was not intended to run as-is. You will need to modify it for your field(s) and remove the bits you don't need (e.g. start < end comparison.)


Harneet Sital
Mega Sage
Mega Sage

Hi,



Rather than client script use the following UI policy with the following condition :



Capture.PNG



Write following script :


function onCondition() {


      g_form.setValue('date_of_joining', "'');


      alert("Enter valid date");


}



Its simple and works in all the cases.