Dynamically format datetime by locale in Legacy UI JSP


how can I dynamically format datetime values in a jsp (legacy UI) in the format defined by the current locale ?

For example with locale set to “en” the timepicker.js outputs “03/30/2016 12:18 PM” and so does <spring:bind path="effectiveStartDate">${status.value}</spring:bind>. And the format changes according to the configured locale, so thats what I want!

In my case I am showing a read-only list where I cannot use <spring:bind path> and timepicker.js.

I therefore tried <openmrs:formatDate date="${radiologyOrder.effectiveStartDate}" type="short" /> but the available type’s lead to

  • short = 03/30/2016
  • medium = Mar 30, 2016
  • long = March 30, 2016 8:27:00 AM UTC which is different from what <spring:bind path> and timepicker.js do.

Using <openmrs:formatDate date="${radiologyOrder.effectiveStartDate}" format="MM/dd/yyyy hh:mm a" />

I get what I want in the case of locale “en”, but I dont want to hard code the format. The format should be dynamically set from the locale settings.

Can you do?

openmrs:formatDate date="${radiologyOrder.effectiveStartDate}" format="${dynamicFormat}"

And then have your controller do something like: model.put(“dynamicFormat”, someLocateDepedentFormatValue)

thanks for this solution, but I am looking for something even cleaner, dont want to add this to every controller method where I need it. should be done kind of automagically :slight_smile:

create a custom tag

1 Like