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

can you please just have this line and test,


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



are you trying to add 1 day in due date? if this is the case the i think, you can try in this way.



var dateDueDate = current.variables.dueDate;


var dueDate = new GlideDateTime(dateDueDate);


dueDate.addDaysUTC(1);


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



but where is the variable dueDate coming from? it doesn't exist in my form....



i tried this, but it didn't do the trick:




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


var gdt = current.due_date.getGlideObject();


gdt.addDaysUTC(1);



i think what you meant was:




var dateDueDate = current.due_date;


var dueDate = new GlideDateTime(dateDueDate);


due_date.addDaysUTC(1);


current.due_date = due_date.setDisplayValue.split(' ')[0]+ ' ' +'23:59:59';




and yes....I'm trying to add 1 day, and that part is actually working


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');


This worked fine!!!

In my case, original date field was in blank, and I needed to set it up with a new and fixed date.

 

Thx

 

 

This will not work if the user has selected a different display format for dates in ServiceNow (which is uncommon, but could be around 5-10% of your userbase).

I'd recommend this:

var dueDate = new GlideDateTime(current.getValue("due_date"));
var dueDateMidnight = new GlideDateTime();
dueDateMidnight.setDisplayValueInternal(String(dueDate.getLocalDate()) + " 23:59:59");

current.setValue("due_date", dueDateMidnight.getValue());