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

karthik73
Mega Guru

Hi Joe,



What is the data type of the u_date_service_needed field?


Hi Joe,



I guess your field type is date/time, the field type should be date.



i replicated your steps and for the field   as date/time i got the time as 17:00 ( because of my instance being in PST)



find_real_file.png



I have copied the value in the variable date to both the fields shown below activity


Thanks for the reply Karthick.



Changing the data type would work if we don't care about the time piece at all, but would not accommodate the second item I listed in my original question, where we want to set it to some specific time.



In a nutshell, there may be some instances in which we care about the time, and others that we don't.   So I am just trying to figure out how I can program that to change the time portion.



Thanks


karthik73
Mega Guru

Hi Joe,



You can update the time on the field by GlideTime() API



The below script will update the time on the gliddate object to 06:45:20, you can replace that with the time you are looking for. You may need to look at the time zone offset too.



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


var mytime = new GlideTime();


mytime.setValue("06:45:20");


gdt.add(mytime);


gs.print(gdt.getValue());