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

Format datetime for expression widget

Guest

Format datetime for expression widget

Hi all,

 

I need to format current date time on ThingWorx expression widget, but javascript functions do not work without libraries (i.e. dateFormat(newDate(), "dd-MM-YYYY HH:mm")). How can I format datetime with native functions?

 

Thanks,

Elisabetta

 

 

 

6 REPLIES 6

Re: Format datetime for expression widget

You can use Javascript Standard DateTime Format functions, something like:

new Date().toLocaleString()

Re: Format datetime for expression widget

Hello,

 

To expand on Carles' reply:

  1. There's another ugly way of formatting it via date.getFullYear() + "-" + ....
  2. There are ThingWorx widgets which support date formatting on the client side (e.g. Value Display, Grid, ...)
  3. You can pre-format on the server side and return in a temporary INFOTABLE field, e.g. "displayDate"

Carles' suggestion + Option (2) are the best variants, because they take into account user's locale settings.

 

Regards,
Constantine

Re: Format datetime for expression widget

Yup, but I would never use Option 3, as it will be formated at Server TimeZone not at user's Timezone ;)

Re: Format datetime for expression widget

Agree, (3) is a bad idea, it would be the last thing I recommend.

 

Having said that, there are some exceptions:

  1. They define timezone per user explicitly and get into trouble of using it correctly on the server side (often needed when the app sends something outside -- emails, SMS, etc.)
  2. All users are (and will be) in the same timezone and the server can be configured accordingly

As a rule of thumb formatting date/time on the server side is almost always a bad idea. But if (and only if) you have a very clear idea about how it works (like your famous extension), then you can break the rules sometimes :)

Re: Format datetime for expression widget

As you said, if you have to format DateTime on ServerSide, which actually we are doing it (and it's not that strange on the use cases you pointed out - email, notifications,...-) you need an extension in order to format it correctly (my one :P) of course knowing before the right Timezone for the User or for the Thing...

Re: Format datetime for expression widget

Hi Elisabetta,

Thanks for using our Community!  I wanted to follow up with you on your post:  https://community.ptc.com/t5/ThingWorx-Developers/Format-datetime-for-expression-widget/m-p/620719#M...

 

Did either Constantine or Carles's replies answer your question?  If so, it would be great if you could mark the appropriate reply as the Accepted Solution for the benefit of other Community members who may have the same question.

 

If not, please respond to the thread and let us know your current status so that we can support further.

 

Thanks!

Leigh