Compare start date and current date & time

Shasu
Tera Contributor

Hi All,

 In change request ,

1)change request type is not emergency.

2)if start date field is less than current date , display an error message ('Planned start date should always be a future date') (but it allow the current date )

3) if start date field time is less that current time ,then display an error message('Planned start date should always be a future time') 

I want to compare both the date and time in start date field with current date &time , but current date should be allow in start date field & start date time should be greater that current time .

Can someone help me with this?

2 REPLIES 2

johnfeist
Mega Sage
Mega Sage

Hi Shasu,

 

If you take a look at the client scripts on change_request, you will see one called Planned Start Date onChange validation which is OOTB.  Beyond that, you need to setup your client script to do an AJAX call to a Script Include (Client callable) and pass the value the user entered.  From there the script include does the work of comparing the two date times in question.

 

compareDates : function() {
   var inputdate = this.getParam("startDate");
   var theStart = new GlideDateTime(inputDate);
   var now = new GlideDateTime();
   var theDif = gs.dateDiff(now, theStart, true);
   if (theDif < 0) {
      return false;
   } 
   return true;
   }

I haven't run this script so there's probably a fat finger in it, but this should get you going.

 

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

SAI VENKATESH
Tera Sage
Tera Sage

Hi @Shasu 

You can add change is not emergency in business rules trigger condition with before insert,update

 for the 2 nd question You can use the below script:

 

(function executeRule(current, previous /*null when async*/) {

    var actualstart = new GlideDateTime(current.work_start); 
    var currentdate = new GlideDate().getDate();
    
    if (actualstart < currentdate && actualstart != currentdate) {
        gs.addErrorMessage("Planned start date should always be a future date.");
    }

})(current, previous);

 

 

for 3 rd question

 

(function executeRule(current, previous /*null when async*/) {

    var actualstart = new GlideDateTime(current.work_start); 
    var currentDateTime = new GlideDateTime();
    
    var currentDate = new GlideDate().getDate();
    var currentTime = currentDateTime.getLocalTime(); 
    
    if (actualstart.getDate() < currentDate || (actualstart.getDate() == currentDate && actualstart.getLocalTime() <= currentTime)) {
        gs.addErrorMessage("Planned start date should always be a future time.");
    }

})(current, previous);

 

 

Thanks and Regards

Sai Venkatesh