- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2017 03:41 PM
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);
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2017 02:46 PM
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');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2017 08:17 PM
Hi,
Please use this snippet.
var result = "";
if(format.indexOf('h') < 0 && format.indexOf('a') < 0)
result = "Due date proposed: " + current.variables.due_date + "11:59";
else
result = "Due date proposed: " + current.variables.due_date + "23:59";
Thanks
- YLN
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2020 06:23 AM
Seems weird there's no other way to do this without parsing strings.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2021 01:12 PM
I recently had this issue and came up with the following solution which only uses documented methods and a minimal amount of string spaghetti:
function setTime(oldDate, time) {
var newDate = new GlideDateTime('1970-01-01 ' + time);
if (!newDate.isValid()) {
throw new Error('Invalid time provided: ' + time);
}
newDate.setYearUTC(oldDate.getYearUTC());
newDate.setMonthUTC(oldDate.getMonthUTC());
newDate.setDayOfMonthUTC(oldDate.getDayOfMonthUTC());
return newDate;
}
You can use it like this:
var todayAtNoon = setTime(new GlideDateTime(), '12:00:00');
which would get you a GlideDateTime representing today at noon.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2022 02:38 AM
Old post new script. This one preserves the locality of the time as often we want a local representation of time so it's consistent when comparing with other objects in the system.
var myDateTimeField = ; //Set variable to the date/time field you want to change
var time = ; //Set the time as a string, i.e. "12:30:00"
var newDateTime = setTime(myDateTimeField, time);
var localDateTime = newDateTime.getDisplayValue();
function setTime(DateTime, time) {
//Convert to GlideDateTime
var oldDateTime = new GlideDateTime(DateTime);
var gtime1 = new GlideTime();
gtime1.setValue(time);
var newDateTime = new GlideDateTime();
//Preserves the date in local time but sets the time portion to 00:00:00
newDateTime.setDisplayValue(oldDateTime.getLocalDate());
//Add the time portion on
newDateTime.add(gtime1);
return newDateTime;
}