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

Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X

Format datetime for expression widget

efasano
4-Participant

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

 

 

 

5 REPLIES 5

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

new Date().toLocaleString()

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

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

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 :)

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...

Top Tags