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

Changing Time Component from Service Catalog Variable Set by DatePicker

jmiskey
Kilo Sage

In one of our Service Catalogs, we have variable that is set to a Date Picker.   In our workflow, the variable is copied over to a custom field we added to our Request Item table.   That script looks like this:

var dteServiceNeeded = current.variables.DateServiceNeeded;

current.u_date_service_needed = dteServiceNeeded;

So when I test it out, and view the custom field on my Request Item table, it always shows whatever date I have chosen with a time component of 8:00 PM (not sure why it is picking that time).

So, I have a few question regarding this time piece:

1.   How can I remove it, so it does not have a time component (or has a time component of 12:00 AM)?

2.   How can I change it to use some other pre-determined time (like if I want them all to day 6:30 AM going forward)?

It is not a matter of just "hiding" the time component through formatting.   I actually need it removed/changed, as it may be used in some calculations.

I found some old discussions that talked about using g_form.GetDisplayValue along with the Split function, but they only seemed to return errors for me.

Thanks

1 ACCEPTED SOLUTION

Hi Joe, the below script gets the time zone offset and adds to the variable and updates   to the field.



I just remember the problem i faced some time back between the variable date and field date, variable dates are   stored as UTC , while the field dates are stored in


local time zone.



The below script works fine, tested:)



var gdt = new GlideDateTime(gr.variables.date.toString())


// get the timezone offset in milliseconds and adds to your time value, this gives you the midnight time of the date picked in variables


gdt.add(gdt.getTZOffset()*(-1));


var myTime = new GlideTime();


// here you add the time you need to custom add


myTime.setValue("16:44:00");


gdt.add(myTime);


// update the field value


gr.u_datetime = gdt;


View solution in original post

13 REPLIES 13

I found a thread that looks like it works in Offset, but still have not quite gotten it to work out yet.


Issue with timezone (offset); variable contains wrong value


OK, I was able to cobble some code together based on some code I found in our system and in the community.


Here is the code that I came up with that finally does what I need:



//store entered date in Glide record


var dtSvNd = new GlideDateTime(current.variables.DateServiceNeeded);


//add one day because eastern time zone is minus 4 or 5 hours, and if no time in date field, takes us back one day


dtSvNd.addDaysLocalTime(1);


//get date portion


var datePart = dtSvNd.getLocalDate().getByFormat('MM-dd-yyyy');


//add time component


var timePart = ' 04:44:00 PM';


//set date_service_needed field on sc_req_item


current.u_date_service_needed = datePart + timePart;


Hi Joe, the below script gets the time zone offset and adds to the variable and updates   to the field.



I just remember the problem i faced some time back between the variable date and field date, variable dates are   stored as UTC , while the field dates are stored in


local time zone.



The below script works fine, tested:)



var gdt = new GlideDateTime(gr.variables.date.toString())


// get the timezone offset in milliseconds and adds to your time value, this gives you the midnight time of the date picked in variables


gdt.add(gdt.getTZOffset()*(-1));


var myTime = new GlideTime();


// here you add the time you need to custom add


myTime.setValue("16:44:00");


gdt.add(myTime);


// update the field value


gr.u_datetime = gdt;


Yes, that way seems to work too.   Here is how I adapted it for my situation (just missing a semi-colon at the end of the first row, and then updated for my variables):


var gdt = new GlideDateTime(current.variables.DateServiceNeeded.toString());
// get the timezone offset in milliseconds and adds to your time value, this gives you the midnight time of the date picked in variables
gdt.add(gdt.getTZOffset()*(-1));
var myTime = new GlideTime();
// here you add the time you need to custom add
myTime.setValue("16:44:00");
gdt.add(myTime);
// update the field value
current.u_date_service_needed = gdt;



I kind of like applying the current offset dynamically instead of hard-coded to subtract one day.   Seems a bit safer and dynamic to use on other times (if it was not starting at midnight).



Thanks!