Skip to main content
18-Opal
October 14, 2021
Solved

UTC to CDT Timezone

  • October 14, 2021
  • 2 replies
  • 2689 views

Hi,

How to convert the UTC Date Time to CDT in thingworx ?

@paic @c_lowy @slangley 

Thanks,
Shashi

Best answer by pshashipreetham

Hi,

After lot of manipulation in the TimeStamp with Thingworx and JS Functions was not able to get proper time zone, so used the Third Party API for this. Use the below Code:

var params = {
 proxyScheme: 'http' /* STRING */,
 headers: {"Accept-Encoding":"gzip, deflate, br","Accept":"*/*","Content-Type": "application/json"},
 ignoreSSLErrors: true,
	url: "http://worldtimeapi.org/api/timezone/America/Chicago" /* STRING */,
};

// result: JSON
var CDTdate = Resources["ContentLoaderFunctions"].GetJSON(params);
//date = CDTdate.datetime;
date = ((CDTdate.datetime).split("T")[0]);
time = ((CDTdate.datetime).split("T")[1]);
timestamp = ((time).split(".")[0]);
hours = timestamp.split(":")[0];
if(hours > 12 ){
 result = date + " " + timestamp + " PM CDT";
}
else{
 result = date + " " + timestamp + " AM CDT";
 }

 

Thanks,
Shashi

2 replies

22-Sapphire I
October 14, 2021

If you have a 'fixed' offset, you can just apply the offset.

If you don't know the offset, you'll somehow have to determine the 'locale' and then apply the offset.

Since client side automatically converts to client side local time, you'll also have to keep that in mind when doing the offset.

I recommend a simple google search for javascript converting date time to another timezone

 

If it is a fixed offset though, there are the date time code snippets so you can do 'datetimeaddhours'

18-Opal
October 14, 2021

Hi @PaiChung ,

Thanks for Quick Response, but tried in all ways, nothing helped, therefore reached out for the community !!

Thanks,
Shashi

22-Sapphire I
October 14, 2021

Can you perhaps describe in more detail the situation you are facing?

When and where do you receive/have the data stored that needs to be converted, as well as for what reason?

pshashipreetham18-OpalAuthorAnswer
18-Opal
November 27, 2021

Hi,

After lot of manipulation in the TimeStamp with Thingworx and JS Functions was not able to get proper time zone, so used the Third Party API for this. Use the below Code:

var params = {
 proxyScheme: 'http' /* STRING */,
 headers: {"Accept-Encoding":"gzip, deflate, br","Accept":"*/*","Content-Type": "application/json"},
 ignoreSSLErrors: true,
	url: "http://worldtimeapi.org/api/timezone/America/Chicago" /* STRING */,
};

// result: JSON
var CDTdate = Resources["ContentLoaderFunctions"].GetJSON(params);
//date = CDTdate.datetime;
date = ((CDTdate.datetime).split("T")[0]);
time = ((CDTdate.datetime).split("T")[1]);
timestamp = ((time).split(".")[0]);
hours = timestamp.split(":")[0];
if(hours > 12 ){
 result = date + " " + timestamp + " PM CDT";
}
else{
 result = date + " " + timestamp + " AM CDT";
 }

 

Thanks,
Shashi