- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2017 12:47 PM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2017 12:27 PM
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2017 01:03 PM
Hmmm.... I must not be applying this right, as it is not working for me.
Here is how I tried to apply it (note that my Service Catalog Variable is a "Date" field, not a Date/Time field, but the Date field on my Request Item table is a Date/Time field.
var dteServiceNeeded = current.variables.DateServiceNeeded;
var myTime = new GlideTime();
myTime.setValue("16:44:00");
dteServiceNeeded.add(myTime);
current.u_date_service_needed = dteServiceNeeded;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2017 01:17 PM
Hi Joe,
You are missing the GlideDateTime object, try as below.
var dteServiceNeeded = new GlideDateTime(current.variables.DateServiceNeeded);
var myTime = new GlideTime();
myTime.setValue("16:44:00");
dteServiceNeeded.add(myTime);
current.u_date_service_needed = dteServiceNeeded;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2017 01:25 PM
OK. We are getting closer! It is 4 hours off. It is returning 12:44 PM (instead of 4:44 PM).
I am guessing it may have something to do with the time zone offset. Not really sure how to incorporate that, but I am not sure if we want to anyway. Since we have daylight savings time, that value changes depending on the time of the the year.
Is there some way to get rid of whatever time it has, prior to adding the time with the .add(myTime) statement, i.e. initialize it 12:00 AM.
That may be handy to know anyhow because I envision having to adjust some date/time values to 12:00 AM in the future for other processes.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2017 01:32 PM
if you want the time as 12:00 AM you can get using the getMidnight function
var gdt = new GlideDateTime("2017-03-16 18:00:00");
gs.print(gdt.getInternalMidnight(gdt.getDayOfWeek()));
in your script it should be
var dteServiceNeeded = new GlideDateTime(current.variables.DateServiceNeeded);
current.u_date_service_needed = dteServiceNeeded.getInternalMidnight(dteServiceNeeded.getDayOfWeek());
You should be able to set the Timezone offset with the offset methods available
GlideDateTime - ServiceNow Wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2017 09:37 AM
I thank you for your patience and efforts so far. I just cannot get this to work the way that I need it to. I am obviously not understanding how this works (to me, it seems like it is way more complex than it needs to be - I could do this very easily in other programming languages).
My thinking was that if I strip the time off so it goes back to midnight, then I should be able to simply add the time value that I need to get the date/time that I want.
So, for my example, if I select the date April 1, 2017 from my Service Catalog form, the calculated date/time I want back is April 1, 2017 4:44 PM.
I tried incorporating the methods you gave me like this:
var dteServiceNeeded = new GlideDateTime(current.variables.DateServiceNeeded);
var date1 = new GlideDateTime();
date1 = dteServiceNeeded.getInternalMidnight(dteServiceNeeded.getDayOfWeek());
var myTime = new GlideTime();
myTime.setValue("16:44:00");
date1.add(myTime);
current.u_date_service_needed = date1;
But when I select April 1, 2017 from my Date Picker, the value it is calculating and returning is actually March 31, 2017 12:44 PM! So not only is it the wrong time, it is also returning the wrong date!
I am guessing that the Time Zone Offset you mentioned is somehow coming in to play, but I thought the method I was trying to incorporate would bypass that by setting time to midnight and adding the time, but it looks like we still need to account for the time zone offset. But I cannot figure out how to do that. I have searched around for examples, but have not found anything yet.