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

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

how to convert a string into datetime using service in thingworx?

magrawal
2-Explorer

how to convert a string into datetime using service in thingworx?

Hi,

I have a use case where I am receiving following JSON payload(example payload) into loadJSON property.

{"time":"2016-08-02 19:00:00","temperature":Temp}

I have created following two logged properties in my Thing(DemoThing) followed by generic ThingTemplate :
1)Timestamp(BaseType DateTime)

2)and Temp(BaseType Number)

Now, to retrieve values for above mentioned properties from JSON payload I wrote following script in my service

Things["DemoThing"].Temp=Things["DemoThing"].loadJSON.temperature

Things["DemoThing"].Timestamp=Things["DemoThing"].loadJSON.time

when I tried to execute service I received following error:
Unable To Convert From java.lang.String to DATETIME Cause: Unable To Convert From java.lang.String to DATETIME]


So I modified Service as following:

Things["DemoThing"].Temp=Things["DemoThing"].loadJSON.temperature

var timeString= Things["DemoThing"].loadJSON.time

Things["DemoThing"].Timestamp=new Date(timeString)

Now Service executed without any error but values of properties set as:
1)Timestamp: 1970-01-01 0:00:00 (not correct, I was expecting 2016-08-02 19:00:00)

2) Temp: Temp (expected)

now I am not getting how to convert a string into datetime using service in thingworx?

so I am looking for a help as soon as possible.

Thanks in advance

Meenakshi

ACCEPTED SOLUTION

Accepted Solutions
ttielebein
14-Alexandrite
(To:magrawal)

Hello,

Here is the syntax you are looking for:

var json = {"time":"2016-08-02 18:45:00","temperature":30};

me.MyDate = parseDate(json.time,"yyyy-MM-dd HH:mm:ss"); //MyDate is a datetime type property on my thing

Hope this helps!

Tori

View solution in original post

6 REPLIES 6
ttielebein
14-Alexandrite
(To:magrawal)

Hello,

Here is the syntax you are looking for:

var json = {"time":"2016-08-02 18:45:00","temperature":30};

me.MyDate = parseDate(json.time,"yyyy-MM-dd HH:mm:ss"); //MyDate is a datetime type property on my thing

Hope this helps!

Tori

Hey,
Thanks for replying but the date value I am receiving as an Output from the above function is  2016-08-03 04:15:00.000.
(Input date was: 2016-08-02 18:45:00)
Don't know what is wrong here.

ttielebein
14-Alexandrite
(To:magrawal)

What version of ThingWorx are you using? It could possibly be modifying it to sync with system time, or something odd like that. The difference between the input and the date you say it is returning is exactly that between IST and EST. 2016-08-02 18:45:00 EST == 2016-08-03 04:15:00 IST

I do not know if it is supposed to do this or not, so if you tell me the version, I can look into this further. Did you copy and paste my syntax exactly? If not, also please comment your exact script here.

Thanks!

Tori

I am working on Thingworx version 6.6

Yes, I executed your syntax exactly.

Added Information: My thingworx server is in EST Timezone.

Thanks,

Meenakshi

Hi,

parseDate it's executed on Server Side, then when you parseDate you should pass the String as server side TimeZone. If you have the remote device, or the user on another TimeZone then you will need to know the TimeZone offset to add to it after parsed.

We have all the servers at Zulu Time ( +00:00 Offset ) this makes it easier to work.

Carles.

Thanks carles!

It's helpful

Announcements


Top Tags