How to show a datetime in specific timezone, CET for instance?

formatDateTime and formatDateTimeUTC do show your localized time and the UTC time respectively, but how can you show the current time another timezone?  
5 answers

This is a great question. 

I have built a Java action that can behave in a similar way to the built in formatDateTime method, but also take a time zone string parameter. e.g. ‘Europe/London’, and return the time from another time zone to the user.

This uses the built in Java time and timezone classes, no external API necessary. It does require Mendix 8.6.9 or above though.

It’s available on my GitHub.

If you don’t want the example project and unit tests, just download the stand alone module from the releases folder and import into your own project.

I’m thinking of refining this a bit more and releasing it on the Marketplace.

I know dates and times have lots of gotchas, so I’m interested in any feedback / bugs.


A couple of ideas:

  • Adjust the UTC time with the addHours function to reach the desired timezone. You could do this programmatically by fetching the appropriate System.Timezone object and using its offset attribute.
  • A widget could be useful for to do the translation directly on the browser-side. I think there’s a localize function somewhere in the Mendix client API

I don't think there's a built-in way sadly, and (as I'm sure you've already considered) it's not as easy as just adding x hours to the UTC time as you might need to deal with summertime / wintertime and such. Looked into it, and this free timezone API might work if you don't need too many requests at a time?

Edit: Alternatively, combine these approaches to figure out summer / wintertime with the API (call it once per hour for the timezones you need to check out the time difference to UTC) and then use the addHours approach.


Edit:  There are several Java libraries available like java.time.* to convert dates to different timezones. 



Other than trying to build one’s own logic, one option is to use the API for time conversion, I definitely would use these, if the transaction volume is going to be low.