Skip to main content
1-Visitor
July 30, 2019
Question

Format datetime for expression widget

  • July 30, 2019
  • 2 replies
  • 3007 views

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

 

 

 

2 replies

1-Visitor
July 30, 2019

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

new Date().toLocaleString()
18-Opal
July 31, 2019

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

1-Visitor
July 31, 2019

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

18-Opal
July 31, 2019

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 🙂