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 |
|
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" |
|
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 |
|
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 |
|
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 |
|
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" |
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
Comments
0 comments
Article is closed for comments.