The CreatorCon Call for Content is officially open! Get started here.

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

lakshminarayan4
ServiceNow Employee
ServiceNow Employee

Hi,


Please use this snippet.




var format = gs.getUser().getDateTimeFormat() + "";


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


MG Casey
Mega Sage

Seems weird there's no other way to do this without parsing strings.

Eric Medina
Tera Contributor

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.

Tim Grindlay
Mega Sage

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;
    }