Date comparsion and validation within Catalog Items.

peterwigham
Kilo Expert

Hi guys,

 

I'm hoping someone can help me with this as I've been hitting my head against a brick wall all day.

 

I have a Catalog Item with start and end date variables (sDate and endD), both of which are Date type variables. What we need is a validation to check that the sDate value is a date in the future and that endD value to check to ensure it is later (in the future) than the sDate value that's entered.

 

I've looked within the wiki and Community and there appear to be lots of information on this however nothing I've tried seems to have worked. I've tried simple Catalog Client Scripts to perform this but they have not worked. I've tried to then use Script Includes with Client Scripts but these do not seem to work properly for me either. The issues I've had are:

 

- Populating the the sDate with a value forces the alert to display where the date is in the past of the future. If date is TODAY then it seems to show NO alert.

- Alert displaying when populating the endD variable whether the value is later in the sDate variable value or not.

 

In an attempt to debug I thought that there may be an issue with obtaining the current date and so I used a Script Include to get this and used it as a variable to populate the start date. In some cases this worked but in others the value merely populated as 'null'.

 

Some of the articles I've checked (and used) but that have not resolved this issue are:

 

Date Validation script needed for Catalog Item date variable.

Comparing Client Dates

Validate a date via a client script

 

Any help on this would be hugely appreciated.

 

Thanks in advance,

PW

1 ACCEPTED SOLUTION

KHDouglas
Giga Expert

Hi Peter,



This can be done using 2 onChange client scripts, both in attached txt doc.



Karl


View solution in original post

9 REPLIES 9

sach1
Tera Guru

Hi Peter,



I have spent a lot of Time in doing this.


My Start Date was supposed to One week after from the present date. So while validating I used the Date functions.


But the problem was that date functions were changing the date format from dd-MM-yyyy to yyyy-MM-dd. So I used array to Invert, this may not be the best practice but seeing it work made me happy.



below is the Script Include. You can pass dates from On Change Client script.



CheckStartDate: function() {



  var start = this.getParameter('sysparm_date');


  gs.log (start, "start");


  var DayOneWeek = new GlideDate();


  //var DayOneWeek = gs.daysAgo(-7);


  DayOneWeek.addDays(+7);


  gs.log(DayOneWeek , "rental default date");



  var arr = start.split('-');



  var startInvert = arr[2] + "-" + arr[1] + "-" + arr[0];



  gs.log(startInvert , "rental format");



  var diff = gs.dateDiff(startInvert, DayOneWeek, false);


  gs.log(diff , "difference");


  if(startInvert < DayOneWeek){


  return false;


  }else{


  return true;


  }


  },


KHDouglas
Giga Expert

Hi Peter,



This can be done using 2 onChange client scripts, both in attached txt doc.



Karl


First I would like to thank you KArl.



It does work for me too. I also needed the same functionality. But somehow the script include was not able to check in the case when selected date and current date were equal. I am wondering how to do this in the script include so that if I want to incorporate the functionality in more than one record producer I can just call the script include.



Any help would be appreciated.


Thankyou Karl.. The codes in the document were very useful and worked at one go..