How to check if start date in change is after today?

Khushboo8
Tera Contributor

How to check if start date in change is after today?

3 REPLIES 3

snehabinani26
Tera Guru

HI Khushboo,



This query can help you



start_date>javascript:gs.daysAgoEnd(0)



find_real_file.png


Ankur Bawiskar
Tera Patron
Tera Patron

Hi Khushboo,



If it is only a date field and you want to check something in client script then have onChange client script on that field i.e start date and have following script.


you can even show an alert if you want as mentioned in sample script below:



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


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


          return;


    }



var startDate = new Date(newValue);


if(startDate <= new Date()){


alert('this is a previous date');


}


else{


alert('this is an ahead date');


}


}



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


Thanks


Ankur


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

bubuprasadswain
Tera Guru

write a OnChange Client script for End date field with below script:


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


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


          return;


    }


    //Type appropriate comment here, and begin script below


    var end = g_form.getValue("u_glide_date_time_2");


  var start = g_form.getValue("u_glide_date_time_1");


  // skip if start or end is blank


  if (start == "" || end == "") {


  return;


  }


    // get user's date time display format


  var format = g_user_date_time_format;


  var isEndBeforeStart = compareDates(start, format, end, format);



  // 1 (true) if start is greater than end


  // 0 if end is greater than start of if they are the same


  // -1 if either of the dates is in an invalid format



  if (isEndBeforeStart > 0) {


  alert("End must be after start");


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


  return false;


  }


}



Regards,


Bubuprasad