ng:dateFormat

ng:dateFormat

This section explains the built-in XPath function: ng:dateFormat(dateString, inputPattern, outputPattern, locale, customErrorMessage)

 

This function returns a formatted date string, formatted using the Java SimpleDateFormat function (like InterFormNG date formatting).

 

If you want to use the timestamp (current date and time), then you should consider the standard current-date() and current-dateTime() XPath functions.

 

An alternative to this function is ng:dateTimeFormat. It is a very good idea to consider the ng:dateTimeFormat function even if you only want to convert the date itself as the ng:dateTimeFormat can convert the format of the date depending on the locale e.g. rearrange the sequence of the month, day and year depending on the locale.

 

It has these parameters:

 

dateString

The input date, that is to be formatted. If this is empty (null) then no output is generated.

 

inputPattern

This is optional input, that specifies how the input dateString is already formatted. If not specified, then "yyyy-MM-dd" is assumed.

Possible formats are listed here: https://docs.oracle.com/javase/10/docs/api/java/text/SimpleDateFormat.html

 

outputPattern

Optional input that specified how the output date string should be formatted. If not specified, then "yyyy-MM-dd" will be used.

Possible output formats are listed here: https://docs.oracle.com/javase/10/docs/api/java/text/SimpleDateFormat.html

 

locale

Optional input specifying the locale to use when creating the string. If this parameter is not specified, then the locale setup on the template settings is used if used in a template (en-GB is used by default in the workflow).

Possible values for the locale can be found on the drop down list on e.g. the Translation element.

You can also find a list of locales here: https://www.oracle.com/java/technologies/javase/jdk8-jre8-suported-locales.html

 

customErrorMessage

Optional input which specifies the value to return, if the function cannot successfully convert the input date to the requested format (e.g. if the first parameter is not a valid date). If not specified InterFormNG2 will return the Xpath specifications.

 

The 3 optional parameters are filled out from left to right, so if you e.g. want to state a locale, then you also need to select an InputPattern and an outputPattern.

 

Please notice, that ng:dateFormat follows the rules of the link to the date formats above, so you should look out for these details:

 

In the format you need to specify a capital ‘M’ for the month. (A small, ‘m’ is the minute of the hour, so you probably do NOT want that..).

‘E’ selects the day of the week.

The length of identical characters sets the ‘length type’ e.g.:

‘E’, ‘EE’ or ‘EEE’ means ‘Fri’,

‘EEEE’ means ‘Friday’.

‘M’ means the month without preceding ‘0'.

‘MM’ means the month with preceding ‘0' for months 1-9.

‘MMM’ means the short name of the month e.g. ‘Jan’

‘MMMM’ means the long name of the month e.g. ‘January’.  

 

 

Examples:

 

ng:dateFormat('24.12.2020','dd.MM.yyyy','dd. MMMM yyyy')

This output this: 24. December 2020

 

ng:dateFormat('4-08-2015','dd-MM-yyyy') outputs 2015-08-04

 

ng:dateFormat('4-08-2015','dd-MM-yyyy','MM') outputs 08

 

ng:dateFormat('24-08-2015','dd-MM-yyyy','EEEE') outputs Sunday

 

ng:dateFormat('24-08-2015','dd-MM-yyyy','EEEE','da-DK') outputs søndag

 

 

The ng:dateFormat require, that the year of the input date specifies the year including the century e.g. 2022 - not 22.

 

If you have an input date in a format, where the year is only shown with 2 digits e.g. '12/31/22' to indicate the last day of the year (dd/MM/yy), then you can use this expression to present the date in another format (assuming the century of the date to 20):

 

ng:dateFormat(concat(substring($date,1,string-length($date)-2),'20',substring($date,string-length($date)-1)),'dd/MM/yyyy','dd MMMM yyyy')

 

The expression above consists of these elements:

 

This expression extracts the input date - except the year (which is found in the last 2 characters):

substring($date,1,string-length($date)-2)

 

This expression extracts the last 2 characters of the input date - which is the 2 digits containing the year:

substring($date,string-length($date)-1)

 

The concat merge the initial part of the input date followed by '20' (for the century) and the 2 digits of the year.

 

    • Related Articles

    • ng:dateTimeFormat

      This section desribes the built-in function, ng:dateTimeFormat(xmlDateTime, dateFormat, part, locale, customErrorMessage) The function converts an XML date and time (timestamp) into the format of your choice. An alternative to this function is ...
    • ng:resourceExist

      ng:resourceExist(resource,path) This function checks, if a resource in InterFormNG2 exists or not. The function returns a boolean: true or false. The ng:resourceExist Xpath function has this format: ng:resourceExist(resource,path), where: resource ...
    • ng:decrypt

      The built-in function, ng:decrypt is linked with the encryption functions: ng:encrypt and the workflow component, Set secure workflow variable. The function, ng:decrypt can decrypt contents, that was previously encrypted with these functions. The ...
    • ng:minimum

      InterFormNG2 includes the special built-in XPath function, ng:minimum. This function returns the smallest value of two numeric arguments. The function only accepts two numeric arguments - no more and no less. Examples: ng:minimum(-14,2) returns 2 ...
    • ng:base64

      The built-in function, ng:base64 converts a string (text) into a base64 based string. This can e.g. be used, if you want to insert any kind of data into an XML node, without substituting any characters, that are not allowed. This function is related ...