Using dateDiff to check if date is in Future?

Rooma1
Tera Contributor

HI All,

 

There is a field on a catalog Item which should accept only future dates. It should not accept Past and the Current date.

Currently, the logic has been only written to throw an error if it accepts past dates. I need to update the logic so that it should throw an error when the user selects the current date as well.

 

Here is the snippet code for your reference.

var MyDayAjax = Class.create();

MyDayAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    // Checks - Date cannot be in the Past

    checkNowDateTime: function() {

        var firstDT = this.getParameter('sysparm_fdt');

        var nowDT = gs.nowDateTime();

        var diff = gs.dateDiff(firstDT, nowDT, false);

        var datesplit = diff.split(' ');

        gs.log('datesplit '+datesplit[0],'Rooma');

        if (datesplit[0].indexOf(":") > -1 || datesplit[0] < 1) {

            return true;

        } else {

            return false;

        }

    },

 

 

Can anyone please help here as I am not much sure about the dateDIff API?

 

Thanks,

Rooma

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

You should use the newer subtract() method instead of dateDiff, but here's the documentation

https://docs.servicenow.com/bundle/washingtondc-api-reference/page/app-store/dev_portal/API_referenc... 

 

You can also do this with a scripted UI Policy for a lower code approach

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-... 

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

You should use the newer subtract() method instead of dateDiff, but here's the documentation

https://docs.servicenow.com/bundle/washingtondc-api-reference/page/app-store/dev_portal/API_referenc... 

 

You can also do this with a scripted UI Policy for a lower code approach

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-... 

Hi @Brad Bowman  ,

 

I tried using scripted UI Policy but when I am adding the condition and saving it, the condition is clearing off but the logic is working.

 

Not sure why the condition is clearing off.

 

Rooma1_0-1720789456404.png