Articles in this section

How to format date and time using FormatDateTime()

About this article

This article explains what the FormatDateTime() function is and provide examples of how it can be used with the binding syntax in smart templates.  

 

Prerequisites

 

 

What is the FormatDateTime() function? 

FormatDateTime() is a function that formats dates and times according to a date format and language. FormatDateTime() can be used with two different syntaxes depending if a date object is already being utilized, or if a text value should be translated into a date object first (typically the case when using external systems). 

 

FormatDateTime() logic

This syntax should be used if the utilized date is already a date object. The Language is designated using a BCP 47 language tag and will determine the language used for expressing days and months.
The Format follows the .NET standard referenced in the article on date and time formats.
(Examples 1 to 6)

Syntax {{FormatDateTime(Date, Format, Language)}}
Input date, text, text
Output DateTime formatted according to format and language

 

 
  • The syntax {{FormatDateTime(Date, Format)}} can also be used if the user profile contains a Language dropdown called DocumentLanguage.
  • To prevent a character (e.g. a slash '/') from being interpreted as a format specifier, you can precede it with a backslash '\', which is the escape character (see example 4).

 

If the source date is of type text should be first converted into a date object, this syntax should be used instead. ResultFormat and SourceFormat follow the .NET standard. ResultLanguage and SourceLanguage are designated by BCP 47 language tags and determine the input and output of days and months.
(Examples 7 and 8)

Syntax {{FormatDateTime(DateTimeText, ResultFormat, ResultLanguage, SourceFormat, SourceLanguage)}}
Input text, text, text, text, text
Output DateTime formatted according to format and region

 

FormatDateTime() function examples

 

Example 1

Using the FormatDateTime() function based on the "Date" question originating from the response form to insert the date formatted according to dddd d MMMM yyyy for the en-US language. 

Syntax  {{FormatDateTime(Form.Date, "dddd d MMMM yyyy", "en-US")}}
Input Form.Date = 2020-11-12
Output  "Thursday 12 November 2020"

 

Example 2

Using FormatDateTime() together with Now() function to insert today's date and format it according to dddd d MMMM yyyy for the nl-NL language. 

Syntax {{FormatDateTime(Now(), "dddd d MMMM yyyy", "nl-NL")}}
Input Form.Date = 2020-11-12
Output "donderdag 12 november 2020"

 

Example 3

Using the FormatDateTime() together with Now() function to insert today's date and format it according to dddd d MMMM yyyy for the DocumentLanguage set in the user profile.

Syntax {{FormatDateTime(Now(),"dddd d MMMM yyyy", DocumentLanguage)}}
Input

Today's date = 2020-11-12

DocumentLanguage = "nl-NL"

Output "donderdag 12 november 2020"

 

 
  • The binding {{FormatDateTime(Now(),"dddd d MMMM yyyy")}} can also be used if the user profile contains a Language dropdown called DocumentLanguage.
  • In the example above the formatting will always be "dddd d MMMM yyyy".
    If you want the formatting to be language-dependent then use the Translate() function (see example 4 in that article).

 

Example 4

Using the FormatDateTime() function without the "\" escape character in the date format, the output will show a "-" when the language is Dutch ("nl-NL") and a "." when the language is German ("de-DE"):

Binding {{FormatDateTime(Form.Date, "d/M/yyyy", DocumentLanguage)}}
Input Form.Date = 2022-3-17, DocumentLanguage = "en-US" Form.Date = 2022-3-17, DocumentLanguage = "nl-NL" Form.Date = 2022-3-17, DocumentLanguage = "de-DE"
Output 17/3/2022 17-3-2022 17.3.2022

 

If you want the date format always to show a "/" in the output, you have to use the "\" escape character in the binding:

Binding {{FormatDateTime(Form.Date, "d\/M\/yyyy", DocumentLanguage)}}
Input Form.Date = 2022-3-17, DocumentLanguage = "en-US" Form.Date = 2022-3-17, DocumentLanguage = "nl-NL" Form.Date = 2022-3-17, DocumentLanguage = "de-DE"
Output 17/3/2022 17/3/2022 17/3/2022

 

Example 5

Using FormatDateTime() together with Switch() function to show 'st', 'nd', 'rd' or 'th' behind a date, depending on the day.

Binding {{FormatDateTime(Form.Date, "dddd, d", "en-US")}}{{Switch(FormatDateTime(Form.Date, "dd", "en-US"),"01","st","02","nd","03","rd","21","st","22","nd","23","rd","31","st","th")}} {{FormatDateTime(Form.Date, "MMMM yyyy", "en-US")}}
Input Form.Date = 2021-10-1 Form.Date = 2021-10-12 Form.Date = 2021-10-23
Output Friday, 1st October 2021 Tuesday, 12th October 2021 Saturday, 23rd October 2021

 

 
  • Note that in the second "FormatDateTime" function a double dd is used (that searches for '01', '02' etc.). You need to use a double dd because a single d (without MMMM or yyyy) will return a short date.

 

Example 6

Using the FormatDateTime() together with Now() function  to show the current time.

  • h = hour, using a 12-hour clock from 1 to 12
  • hh = hour, using a 12-hour clock from 01 to 12
  • H = hour, using a 24-hour clock from 0 to 23
  • HH = hour, using a 24-hour clock from 00 to 23
  • m = minute, from 0 to 59
  • mm = minute, from 00 to 59
  • s = second, from 0 to 59
  • ss = second, from 00 to 59
  • fff = milliseconds
  • tt = AM/PM designator
Binding {{FormatDateTime(Now(), "hh:mm tt","en-US")}}
Input Now() = 9:05:24 AM Now() = 2:05:24 PM
Output 09:05 AM 02:05 PM

 

Binding {{FormatDateTime(Now(), "H:mm:ss","en-US")}}
Input Now() = 9:05:24 AM Now() = 2:05:24 PM
Output 9:05:24 14:05:24

 

 
  • In the example above the 'H' defines that a 24-hour clock is used. So even while the language is set to "en-US" the output for the afternoon time will be 14:05:24, not 2:05:24.

 

Binding {{FormatDateTime(Now(), "ddMMyy-HHmmss.fff","nl-NL")}}
Input Now() = 17/11/2021, 9:05:24 AM Now() = 17/11/2021, 2:05:24 PM
Output 171121-090524.123 171121-140524.123

 

 
  • Displaying hours/minutes/seconds only works using the Now() function.
    If you would use e.g. "Form.Date", the result will always return zeros (or '12' if 'hh' is used for hours) as output.
  • The output will be updated (showing the current time) when using the document content updater.
    In order to show a unique number in a document, the Now() function can be used as an alternative to the Ticks() function (Ticks will also generate a unique number, but is not updated when using the document content updater).
  • Instead of using "fff" (for milliseconds), one can also use e.g. "ffffff" for millionths of a second.
  • When using e.g. "ffffff" as format specifier, be aware that if the same binding is used more than once in the document (including writing it to a document property), that the output per binding is different, as Templafy has to process each binding individually.

 

Example 7

Using the FormatDateTime() function to convert a date from a text question with a source format of MMM d'th,' yyyy with language en-US to a result format of dddd d MMMM yyyy with language nl-NL(Dutch)

Binding {{FormatDateTime(Form.SomeDates.Name, "dddd d MMMM yyyy", "nl-NL", "MMM d'th, ' yyyy", "en-US")}}
Input

Form.SomeDates.Name = Oct 22th, 2020

ResultFormat = "dddd d MMMM yyyy"
ResultLanguage = "nl-NL"
SourceFormat = "MMM d'th,' yyyy"
SourceLanguage = "en-US"

Output "donderdag 22 oktober 2020"

 

Example 8

Using the FormatDateTime() function in combination with a HostSystem binding, which is used for e.g. Salesforce, SharePoint or Document Creation Services 1.1. Depending on the type of date used in the external system (e.g. yyyy-MM-ddTHH:mm:ss or yyyyMMdd), the appropriate binding must be used.

Binding {{FormatDateTime(HostSystem.Date, "d MMMM yyyy", "nl-NL", "yyyyMMdd", "nl-NL")}}
Input 20210308
Output "8 maart 2021"

 

Binding {{FormatDateTime(HostSystem.Date, "d MMMM yyyy", "nl-NL")}}
Input 2021-03-08T14:07:05
Output "8 maart 2021"

 

Related articles

 

 

add bindings and adaptive section calculate azure information protection array tech_role
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.