- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2016 10:57 AM
When I convert a variable GlideDate field, which is holding a value of 2016-06-27, it is returning as 2016-06-26 05:00:00 PM. Any idea why this would be?
I am doing this with the following script:
var endDate = new GlideDateTime(current.variables.effective_date); //effective_date is my GlideDate variable
current.work_end = endDate; //work_end is an OOB field on task, which I have on my RITM for this workflow
Thank you
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2016 01:48 PM
Robert,
var gdt = new GlideDateTime();
gdt.getTZOffset();
I work in Mountain Time, but our ServiceNow system time is set for Pacific so mine also also resolves to -2520000 (-7 hours in milliseconds)
My problem, and I think Shane's too, is that I am starting with a GlideDate, e.g. 2016-06-29. I'm performing calculations with this value and using it to update another GlideDate. These calculations involved both GlideDate and GladDateTime variables. If I didn't do this my result was always a day early.
// If the input dp_retention_date is 2016-06-29, gdt is '2016-06-28 17:00'.
var gdt_ret = new GlideDateTime(current.dp_retention_date);
// This adds 7 hours => '2016-06-29 00:00:00'
// If not negated, the result would be '2016-06-28 10:00'.
gdt_ret.add(gdt_ret.getTZOffset() * -1);
Thanks.
ps - How did you format the code in your post?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2016 01:48 PM
Robert,
var gdt = new GlideDateTime();
gdt.getTZOffset();
I work in Mountain Time, but our ServiceNow system time is set for Pacific so mine also also resolves to -2520000 (-7 hours in milliseconds)
My problem, and I think Shane's too, is that I am starting with a GlideDate, e.g. 2016-06-29. I'm performing calculations with this value and using it to update another GlideDate. These calculations involved both GlideDate and GladDateTime variables. If I didn't do this my result was always a day early.
// If the input dp_retention_date is 2016-06-29, gdt is '2016-06-28 17:00'.
var gdt_ret = new GlideDateTime(current.dp_retention_date);
// This adds 7 hours => '2016-06-29 00:00:00'
// If not negated, the result would be '2016-06-28 10:00'.
gdt_ret.add(gdt_ret.getTZOffset() * -1);
Thanks.
ps - How did you format the code in your post?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2016 03:11 PM
I just tried going from a catalog item date variable to the incident form's work_end date/time field, and I see what you are saying, Bradley. I had to use the * -1 trick as well to get the right result.
To format code you need to click on 'Use advanced editor' on top right, then click the >> and select Syntax Highlighting along with the language.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2016 03:20 PM
Could you please explain the need for the -1? I find this timezone offset bit a tad confusing and am still trying to wrap my head around it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2016 03:24 PM
Like this:
var endDate = new GlideDateTime(current.variables.effective_date); //effective_date is my GlideDate variable
endDate.add(endDate.getTZOffset() * -1); // Negate the offset to add time.
current.work_end = endDate; //work_end is an OOB field on task, which I have on my RITM for this workflow
Note the "endDate.getTZOffset() * -1" part. That is Bradley's contribution that makes it work in this situation. It converts the negative number to a positive. It is adding the hours (instead of subtracting them) to the date/time before the displayed field converts them.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-19-2017 11:50 AM
The ServiceNow Wiki content is no longer supported. Updated information about this topic is located here: GlideDateTime
Visit http://docs.servicenow.com for the latest product documentation