Why is a GlideDate of 2016-06-27 returning as a GlideDateTime of 2016-06-26 05:00:00 PM?

SC10
Kilo Guru

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

1 ACCEPTED SOLUTION

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?



View solution in original post

14 REPLIES 14

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?



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.


find_real_file.png


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.


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.


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