cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Thingworx server time different to local time.

SOLVED
Regular Member

Thingworx server time different to local time.

Hello. The problem layout is simple: I have a service that returns the current time. The code would be the following:

var d = new Date();
result = d;

This shows the browser's time (for example, 11:50:23). However, if I execute this:

var d = new Date();
var hour = d.getHours();
result = hour;

It will show me the server's time (which happens to be one hour behind, so it shows me 10).

 

Does anyone have any way of approaching this issue, so it would not display the server time, but actually display the actual present time of the browser? Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Thingworx server time different to local time.

Hello Radu,

 

Unless you implement it, the backend doesn't know anything about your client's timezone. It is not specific to ThingWorx, just the way web apps work. There's nothing in HTTP standard that supports automatic exchange of timezone data. Generally speaking, there are two alternatives:

 

  1. Handle timezones on the server side (often can be avoided, and not recommended unless you really need it). In ThingWorx it usually means making time zone explicitly configurable on the User level, i.e. add the corresponding field to the UserExtensions thing shape, implement some logic for initializing and validating it, create a mashup for selecting it, etc. Once you have it, you'll be able to do all date/time operations in the user's local timezone. You usually go for this option when you need to format your date/time as strings to send them out of the system (email, SMS, log files, etc.) Here we only scratch the surface, because if you operate internationally and want to do it right, you'll also need to store some regional settings like local date/time formats (e.g. YYYY-MM-DD vs MM/DD/YYYY).
  2. Offload all timezone handling to the client side (browser, external mailing system, etc.) In ThingWorx you can do most of the date/time formatting work directly inside your mashups. Just return DATETIME objects whenever possible and make the browser do all formatting work for you. It will use user timezone automatically.

Obviously, option (2) is way simpler and more reliable than (1). 

 

Regards,
Constantine

13 REPLIES 13
Highlighted

Re: Thingworx server time different to local time.

Hi @Radu.

 

Could you please provide the version of ThingWorx you're running?  We would like to do some testing around this.  Also, have you tested with multiple browsers with the same results?

 

Regards.

 

--Sharon

Re: Thingworx server time different to local time.

Hi Radu,

 

ThingWorx Javascript services are always executed on the server.

 

I suspect that your first service is returning a TIMESTAMP primitive type (its value is the number of milliseconds since the Unix Epoch). Knowing that it is a TIMESTAMP,  the Thingworx client (Composer / mashup) is able to render it as a Date using the local timezone.

 

I suspect that your second service is returning a INTEGER or NUMBER primitive type. The Thingworx client (Composer / mashup / ... ) will display this value as it.

 

You have two options :

- return a timestamp (your first service) and extra the hour on the client

- or send the client timezone information to the service as argument

The solution may depend on your client capability (Mashup, REST call, Edge SDK, ...)

Re: Thingworx server time different to local time.

 

Re: Thingworx server time different to local time.

 

Re: Thingworx server time different to local time.

Hello,

 

Thank you for your reply. Hardcoding the time difference on the service is not a good solution, because the server I am working on is different to the server that the service will be deployed on.

 

I thought to use timezone information before to adjust the time. But I'd rather not have the client timezone as a service parameter, instead I'm trying to figure out how to calculate the difference in time between the server time and the local time, and eventually implement that into code. Because the new Date() constructor returns the local time, while the getHours() method returns the server time hour.

Re: Thingworx server time different to local time.

Hello @slangley ,

 

I am using Thingworx 8.4.4. I have also tried on Firefox and Edge, but the problem still persists.

Re: Thingworx server time different to local time.

Hello Radu,

 

Unless you implement it, the backend doesn't know anything about your client's timezone. It is not specific to ThingWorx, just the way web apps work. There's nothing in HTTP standard that supports automatic exchange of timezone data. Generally speaking, there are two alternatives:

 

  1. Handle timezones on the server side (often can be avoided, and not recommended unless you really need it). In ThingWorx it usually means making time zone explicitly configurable on the User level, i.e. add the corresponding field to the UserExtensions thing shape, implement some logic for initializing and validating it, create a mashup for selecting it, etc. Once you have it, you'll be able to do all date/time operations in the user's local timezone. You usually go for this option when you need to format your date/time as strings to send them out of the system (email, SMS, log files, etc.) Here we only scratch the surface, because if you operate internationally and want to do it right, you'll also need to store some regional settings like local date/time formats (e.g. YYYY-MM-DD vs MM/DD/YYYY).
  2. Offload all timezone handling to the client side (browser, external mailing system, etc.) In ThingWorx you can do most of the date/time formatting work directly inside your mashups. Just return DATETIME objects whenever possible and make the browser do all formatting work for you. It will use user timezone automatically.

Obviously, option (2) is way simpler and more reliable than (1). 

 

Regards,
Constantine

Re: Thingworx server time different to local time.

Greetings @Constantine ,

Your answer has been a lot of help, thank you. The problem that I'm dealing with involves option (1), unfortunately, since I have to pass the current date as a string (I have to return it as a string, because I must have the date in the ISO format: YYYY-MM-DDTHH:MM:SS . Only now I noticed that if I return the date as DATETIME, it shows the local time, and when I return the date as a string, it shows the server time), to generate an API link.

 

Could you elaborate with greater detail on how exactly it can be accomplished using option (1)?

Thank you.

Re: Thingworx server time different to local time.

Question: If I do this:

var d = new Date();
result = getUTCHours() - d.getTimezoneOffset() / 60;

it seems to return the exact hour that I intend. Does this work everywhere, though?