The CreatorCon Call for Content is officially open! Get started here.

Displayed DateTime Field Wrong Time Zone

gregcoogan
Kilo Explorer

I am developing a calculated field to be displayed on a page for each outage. Whenever I choose a date and time, the calculated field displays the value 5 hours off, likely an incorrect timezone. Here is an illustration of the difference:

6-11-2015 9-09-13 AM.jpg

My code looks like this:

function onBefore(current, previous) {

    //This function will be automatically called when this rule is processed.

    if (current.end.nil())

    {

  current.u_detailed_message = current.cmdb_ci.name + ': ' + current.message + ' | Begin Time: ' + current.begin + ' | End Time: Unknown';

    }

  else if (!current.end.nil())

  {

  current.u_detailed_message = current.cmdb_ci.name + ': ' + current.message + ' | Begin Time: ' + current.begin + ' | End Time: ' + current.end;

  }

}

Can anyone help correct this? It appears to be five hours off.

I have tried using code such as:

var tz = Packages.java.util.TimeZone.getTimeZone("America/Chicago");  

var time = new GlideDateTime();  

time.setTZ(tz);

time.setValue(current.begin);

time.setValue(current.end);


I used the "time.setValue(current.begin)" in the actual code where I concatenated the fields, but "undefined" displayed instead.

I am not sure which direction I should be headed. Anyone know how to deal with this?


I think I could possibly hard-code subtracting the 5 hour difference, but I don't like the idea.


1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Gregcoogan,



In the first code what you pasted just append getDisplayValue() to the date field what you are fetching. I.e


current.begin.getDisplayValue()


Corrected one is below current.u_detailed_message = current.cmdb_ci.name + ': ' + current.message + ' | Begin Time: ' + current.begin.getDisplayValue() + ' | End Time: Unknown';  


View solution in original post

2 REPLIES 2

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Gregcoogan,



In the first code what you pasted just append getDisplayValue() to the date field what you are fetching. I.e


current.begin.getDisplayValue()


Corrected one is below current.u_detailed_message = current.cmdb_ci.name + ': ' + current.message + ' | Begin Time: ' + current.begin.getDisplayValue() + ' | End Time: Unknown';  


Thank you! It worked. I don't know how I missed that!