Tanmay Mishra
Tanmay Mishra

Reputation: 1

In Freemarker Change output date format irrespective of input format

${(.vars["OCRResponse"].Date)?datetime("ANY RANDOM FORMAT")?string("mm-dd-yy").

Can we use If Else within ?datetime, or can we resolve this by using switch case?

Upvotes: 0

Views: 419

Answers (1)

ddekany
ddekany

Reputation: 31152

If that date format is quite "random", and you need to do this a lot, then you are probably better of writing a freemarker.core.TemplateDateFormat+TemplateDateFormatFactory implementation, do the complex date parsing logic in Java, then register the factory as a "custom date format" (that's a FreeMarker configuration setting), let's say with name "random". Then you can do ${OCRResponse.Date?date.@random?string('MM-dd-yy')}. If you set the date_format configuration setting to MM-dd-yy, then you can even just write ${OCRResponse.Date?date.@random}.

You can find concrete examples of defining a custom formats here: https://freemarker.apache.org/docs/pgui_config_custom_formats.html

Another possibility is to use #if/#elseif/#else of course. If you need to do that on multiple places, then put your parser logic into a #function, where you #return the parsed date. So where you insert a date you just have something like ${parseRandom(OCRResponse.Date)} (here I have assumed that date_format is MM-dd-yy, otherwise add ?string('MM-dd-yy')).

Upvotes: 1

Related Questions