Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Set date to specific time

samuelscott
Tera Expert

Hello,  

I'm trying to automatically set a due date field to whatever day is chosen but I want the time to ALWAYS be at 11:59 pm (last hour of due date). The code I'm using is placed on a task box inside a workflow. The result I'm getting is not what I'm looking for. Does anybody know if this code is the problem?

//current.comments = "Due date proposed: " + current.variables.due_date;   var dateDueDate = due_date.toString()+ ' 23:59:59';     var dueDate = new GlideDateTime(dateDueDate);     var dayOfWeek = parseInt(dueDate.getDayOfWeekLocalTime());     dueDate.setDisplayValue(dateDueDate, "yyyy-MM-dd HH:mm:ss"); dateString = "dd/mm/yyyy";     current.due_date = current.variables.due_date; var gdt = current.due_date.getGlideObject(); gdt.addDaysUTC(1);
1 ACCEPTED SOLUTION

this is the correct answer:


var dateDueDate = current.due_date;


var dueDate = new GlideDateTime(dateDueDate);


var arrDate = [];


arrDate = dueDate.toString().split(' ');


current.due_date.setDisplayValue(arrDate[0]+ ' 23:59:59','yyyy-MM-dd HH:mm:ss');


View solution in original post

13 REPLIES 13

EdwinCoronado
ServiceNow Employee
ServiceNow Employee

If your date field is a Date and Time type of field, it will store the time in UTC time zone. You can do something like:



var dateTimeObj = new GlideDateTime(current.your_date_field);


var myDate = dateTimeObj.getLocalDate() + " 23:59:59");


Hi Edwin,



But where are you setting the due_date field to the new value? And in your code, aren't you dealing with a date/time format all the time and end up adding the " 23:59:59" at the end?




When adding you recommended code as shown below,


//current.comments = "Due date proposed: " + current.variables.due_date;




/*var dateDueDate = due_date.toString()+ ' 23:59:59';


  var dueDate = new GlideDateTime(dateDueDate);


  var dayOfWeek = parseInt(dueDate.getDayOfWeekLocalTime());


  dueDate.setDisplayValue(dateDueDate, "yyyy-MM-dd HH:mm:ss");


dateString = "dd/mm/yyyy";


*/


var dateTimeObj = new GlideDateTime(current.due_date);    


var myDate = dateTimeObj.getLocalDate() + " 23:59:59";




current.due_date = current.variables.due_date;


var gdt = current.due_date.getGlideObject();


gdt.addDaysUTC(1);



I get the following result on the targeted field:



find_real_file.png


It seems as if it is adding the actual string value as an integer rather than concatenating the string


Shishir Srivast
Mega Sage

Please check if this helps.



current.dueDate = current.dueDate.toString().split(' ')[0]+ ' ' +'23:59:59';


Hi Shishir,



When placing your code as shown below, i get the following result.



//current.comments = "Due date proposed: " + current.variables.due_date;




/*var dateDueDate = due_date.toString()+ ' 23:59:59';


  var dueDate = new GlideDateTime(dateDueDate);


  var dayOfWeek = parseInt(dueDate.getDayOfWeekLocalTime());


  dueDate.setDisplayValue(dateDueDate, "yyyy-MM-dd HH:mm:ss");


dateString = "dd/mm/yyyy";


*/




current.dueDate = current.dueDate.toString().split(' ')[0]+ ' ' +'23:59:59';




current.due_date = current.variables.due_date;


var gdt = current.due_date.getGlideObject();


gdt.addDaysUTC(1);



find_real_file.png