compare a date and date/time field

venishawakodika
Giga Contributor

I wanna compare a variable to a system field:

system field - closed (closed_at) type is date/time

variable - Requested end date (requested_end_date) type is date

want the sys_id's of only those tasks(sc_task) where closed date is greater than requested end date (closed > req_end_date)

i am using following code:

var ids = [];

  var gr = new GlideRecord('sc_task');

  gr.query();

  while(gr.next())

  {

  var gdt1 = new GlideDateTime(gr.closed_at.getDisplayValue());

  var requested_end_date = new GlideDateTime(gr.variables.requested_end_date);

  //var result = gs.dateDiff(requested_end_date , gdt1,true);

  if((requested_end_date != '' || requested_end_date != null ) && requested_end_date < gdt1){

  ids.push(gr.sys_id.toString());

  }

  }

THE ABOVE CODE GIVES ME SYS_IDS EVEN IF CLOSED DATE IS LESS THAN REQ END DATE, WHEREAS IT SHOULD RETURN ONLY THE GREATER ONES.

Any help on this pls

1 ACCEPTED SOLUTION

sarfraz3
Mega Expert

hi here you can compare both by considering only the date and not time


you just have to slice the time part and take the date part and pass it to datediff method using ajaxcall... this should work..


View solution in original post

8 REPLIES 8

aniketgore
Giga Expert

One way is that you may convert the date time to a string, and then re convert it to the date. Now you can compare the date fields.


Hi, thank you for your reply, i used .getDate() and then compared only the dates, it works now.


sarfraz3
Mega Expert

hi here you can compare both by considering only the date and not time


you just have to slice the time part and take the date part and pass it to datediff method using ajaxcall... this should work..


Hi, thank you for your reply, i did the same, i used .getDate() and then compared only the dates, it works now.