Skip to main content
1-Visitor
August 7, 2016
Solved

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

  • August 7, 2016
  • 6 replies
  • 10635 views

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

Best answer by ttielebein

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

6 replies

5-Regular Member
August 15, 2016

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

magrawal1-VisitorAuthor
1-Visitor
August 16, 2016

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.

5-Regular Member
August 18, 2016

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

1-Visitor
August 22, 2016

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.

magrawal1-VisitorAuthor
1-Visitor
August 22, 2016

Thanks carles!

It's helpful