compare just the date for a planned change with current date

Malaya
Giga Expert

I am pulling in change records for the week using notification script... what i want to do is, compare the planned start date (just the date not time) to current date and highlight the cell... Issue i am running is that, planned start date field has both date and time but i want just the date to be compared.. Thanks, Malaya

1 ACCEPTED SOLUTION

Thank Jake..



Your piece of code helped me figure out the right piece so thank you for that...



This is what worked for me:



var gdt= new GlideDateTime(gr.getDisplayValue('start_date'));


var startDate = gdt.getLocalDate();



var gdt1= new GlideDateTime(gs.nowDateTime());


var compareDate = gdt1.getLocalDate();



  if(compareDate.compareTo(startDate)==0){


  template.print("<td bgcolor=#cde4f4>");


  template.print(gr.getDisplayValue('start_date'));


  }


  else {


  template.print("<td>");


  template.print(gr.getDisplayValue('start_date'));


  }



Thanks everyone for your inputs..



Thanks,


Malaya


View solution in original post

11 REPLIES 11

Can you please the code your are using now? It should work. This code has been tested. What you are doing here is, you are setting the date value of planned start date to a GlideDateTime object. Then you are setting current date/time value to another GlideDateTime object. Then you compare the date object1 to date object2 and see if both object are equal.


Go thru section 3.18 in the following link.


GlideDateTime - ServiceNow Wiki


template.print("<html><table width=70% border=1><tr bgcolor=#e2e0de><td>Change #</td><td>Change Coordinator</td><td>Description</td><td>Impact to user</td><td>Configuration Item</td><td>Planned Start Date</td><td>Planned End Date</td>");




  baseUrl = gs.getProperty("glide.servlet.uri");  


  var gr = new GlideRecord("change_request");


  gr.addEncodedQuery("u_chg_typeIN0,999^start_dateONThis month@javascript:gs.beginningOfThisMonth()@javascript:gs.endOfThisMonth()");


  gr.orderBy('start_date');


  gr.query();



  if (gr.getRowCount() > 0) {


  while (gr.next()){


  template.print("<tr><td>");


  template.print(" <a href='" + baseUrl + gr.getLink() + "'>" + gr.getValue('number') + "</a>");


  template.print("</td>");


  template.print("<td>");


  template.print(gr.getDisplayValue('u_change_owner'));


  template.print("</td>");


  template.print("<td>");


  template.print(gr.short_description);


  template.print("</td>");


  template.print("<td>");


  template.print(gr.getDisplayValue('u_affected_applications_users'));


  template.print("</td>");


  template.print("<td>");


  template.print(gr.getDisplayValue('cmdb_ci'));


  template.print("</td>");



  var gdt= new GlideDateTime();


  gdt.setDisplayValue(current.start_date);


  gdt.getLocalDate();


  var gdt1= new GlideDateTime();


  gdt1.setDisplayValue(gdt.getLocalDate());


  template.print(gdt1+"</br>");


  var gdt2= new GlideDateTime();


  gdt2.setDisplayValue("2016-08-11 20:00:00");


  gdt2.getLocalDate();


  template.print(gdt2+"</br>");


  if(gdt1.compareTo(gdt2)==0){


  template.print("<td bgcolor=#cde4f4>");


  template.print(gr.getDisplayValue('start_date'));


  }


  else {


  template.print(gr.getDisplayValue('start_date'));


  }



  template.print("</td>");


  template.print("<td>");


  template.print(gr.getDisplayValue('end_date'));


  template.print("</td>");


  }


  }




template.print ("</td></tr></table></html>");


It would be great if you can post the code with better formatting. Looks like the formatting is messed up while pasting here.


Hi,



We have a custom date (u_creat_date) field which needs to inherit the date value from the OOB sys_created_on (Date/Time) field in user's time zone.


Also, value of the custom date should match with the value of the date stored on the sys_created_on field.



EX: If an user in PST timezone has created a record on Dec 07 2016   22:00:00   PST then when an user from Hong kong time zone views


this record he should see the value of the custom date field according to his time zone.



Thanks,


jake_mckenna
ServiceNow Employee
ServiceNow Employee

Change lines 28-44 to read like this:



var gdt= new GlideDateTime(current.start_date);  


var startDate = gdt.getLocalDate();  


var gdt1= new GlideDateTime();  


var currentDate = gdt1.getLocalDate();


template.print(gdt1+"</br>");  


template.print(gdt2+"</br>");  


if(currentDate.compareTo(startDate)==0){  


      template.print("<td bgcolor=#cde4f4>");  


      template.print(gr.getDisplayValue('start_date'));  


}  


else {  


      template.print(gr.getDisplayValue('start_date'));  


}