2

In thingsboard I have a post-processing function for an server attribute update dialog.

update server attribute widget

post-processing date value

post-processing date value fails

I need to convert a text value (entered by the user in a widget) into a unix time stamp (milliseconds precision) to store it into an thingsboard attribute.

I also want do use this function to display the value in a formatted ISO date string. Something linke YYYY-MM-DD hh:mm:ss.

As I understand, the date.getMonth, getFullYear, ... functions are pretty standard for JavaScript. What do I need to do to use them in thingsboard too?

Is there a better way to convert dates?

daur
  • 21
  • 1

1 Answers1

1

You have to call use the new-operator to create a date-object.

See Date - JavaScript | MDN:

The only correct way to instantiate a new Date object is by using the new operator. If you simply call the Date object directly, such as now = Date(), the returned value is a string rather than a Date object.

So instead of

var date = Date(value);

it should be

var date = new Date(value);

However, there is a handy and popular javascript date-library called moment.js. Fortunately it is already bundled with Thingsboard and you can use it in widgets and those post-processing functions.

lupz
  • 3,620
  • 2
  • 27
  • 43