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 has these parameters:

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

  • 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')

outputs: 24. December 2020


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

outputs2015-08-04


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

 outputs08

 

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

 outputsSunday


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

outputssø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: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: -14 ...
    • 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 ...
    • ng:spaceConcat

      The built-in function, ng:spaceConcat(), helps concatenate two strings with a space character automatically inserted between them. Here is an example: This XPath expression: ng:spaceConcat('First name:', 'Jon') outputs this string: First name: Jon ...