Work with Dates in velocity templates

JEMH velocity context, relating to emails (comment header specifically) has $sentDate (if present, reverts to now() if not) as well as $receivedDate.  Presentation of these dates can be altered through helper classes in the velocity context.

Lower down there are several date operators that are hyperlinked to API Documentation so you know what you can do with them:

For example:

1a) $dateFormatter.format($sentDate) 1b) $dateFormatter.forLoggedInUser().withStyle($dateTimeStyle.RSS_RFC822_DATE_TIME).format($sentDate) 1c) $dateFormatter.forLoggedInUser().withStyle($dateTimeStyle.DATE_TIME_PICKER).format($sentDate) 1d) $dateFormatter.forLoggedInUser().withStyle($dateTimeStyle.RELATIVE).format($sentDate) 2) $outlookdate.formatDMY($sentDate) $outlookdate.formatTime($sentDate) 3) $dateformatter.getNow() / $dateformatter.formatIso8601($dateformatter.getNow()) 4) $dateTimes.formatRSS($sentDate)

Generates:

1a) 16/Jan/15 10:47 AM
1b) Fri, 16 Jan 2015 10:47:48 +0000
1c) 16/Jan/15 10:47 AM
1d) Today 10:47 AM
2) 16/Jan/15 10:47 AM
3) Fri Jan 16 10:47:48 GMT 2015 / 2015-01-16T10-47
4) Fri, 16 Jan 2015 10:47:48 +0000

Velocity Custom Field Dates

JEMH can build date objects too, to enable values to be generated for arbitrary dates, e.g.:

Date Time
#set($longVal = $currentMillis.longValue() + 86400000 ) $dateFormatter.format($jemhDateUtils.createDate($longVal))

Example Output:

29/Mar/15 3:41 PM

Date Only
#set($longVal = $currentMillis.longValue() + 86400000 ) $dateFormatter.formatDMY($jemhDateUtils.createDate($longVal))

Example Output:

29/Mar/15

DatePicker custom fields

For further Date formatting options please see:

JEMHDateUtils

since 3.2.6

JEMHDateUtils is another interface written by JEMH to accommodate certain functionality which is not achievable when working with Dates obtained from the email.

Debugging velocity rendered custom fields as above is a bit tedious (issue JEMH-7794 logged to pick and evaluate script against a stored JEMH Test Case email). For rapid testing against a Test Case, you can do the same thing in a Script Field Processor edit window:

The key functionality to note:

  • ZonedDateTime - we are using ZonedDateTime which allows us to retain the time zone aspect along with the date + time. This is vital aspect in case you’d wish to convert this to another time zone (which is what is being done in the above example)

  • Formatter - to format the date in a certain pattern (i.e. 12/02/2020 - 08:29), we have provided the getFormatter(pattern) method which is responsible for returning a DateTimeFormatter. The same can be used to format the ZonedDateTime to any other preferred format

Output:

Creating relative dates in custom fields

@since 3.3.27

This set of methods doesn’t depend on any Jira user in the authentication context, e.g., you want to do date/time manipulation for a custom field default

If you want to use the Jira format:

Related articles