- 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-17-2017 10:51 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2017 11:45 AM
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;
- 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-17-2017 12:39 PM
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!