How to do 'Greater than or equal to' comparison between two Date type of fields?

ashwanikumar
Tera Expert

Hi Friends,

In a workflow I need to add a notification when End Date >= (Now Date + 7 Days) . Basically, if 1 week has passed and no action has been taken then I need to add a notification to approvals to take action. I am able to add the 7 Days to now date by using following code:

var gdt = new GlideDateTime();

gdt.addDays(-7);

var gdt2 = gdt.getDate();

gs.print(gdt2);

Now I   do not know how to compare (Greater than or equal to >=) this date with End date. Any suggestions?

Thanks,

KUMAR

7 REPLIES 7

Kannan Nadar
Tera Guru

Hi,



Convert them to numeric value and then compare.



GlideDateTime - ServiceNow Wiki



Thanks,


Kannan


lakshminarayan4
ServiceNow Employee
ServiceNow Employee

Hi ,


You can use the below script to get the days.







var gdt1 = new GlideDateTime("2017-10-27 09:00:00"); // your end date time


var gdt2 = new GlideDateTime();


var dur = GlideDateTime.subtract(gdt2, gdt1); //the difference between gdt1 and gdt2


//http://wiki.servicenow.com/index.php?title=Scoped_GlideDuration_API_Reference#getDayPart.28.29&gsc.t...


var days = dur.getRoundedDayPart(); // this will give the rounded day value


gs.print(days);







Thanks





This helped tons and worked great.  Thank you!

Deepak Kumar5
Kilo Sage

var gdt= new GlideDate();


gdt.addDays(-7);


gs.print(gdt);


var gdt2 = new GlideDate();


gs.print(gdt2);


//Simple Compare


if(gdt<=gdt2){


gs.print("tt");


}


//Using funcation


gs.print(gdt.compareTo(gdt2));


// Equals :0


// Earlier than :1


// Later than :-1