About this article
FormatDateTime() is a function that formats dates and time according to a date format and language.
Prerequisites
|
Syntax
There are two different syntaxes for FormatDateTime(), one taking 3 parameters and the second taking 5 parameters. The first syntax is if your date is already a Date object such as one that has been converted using DateValue() or coming from a date form field. The second syntax takes text values and converts them to a date object in the background and then formats the date. This is typically used for external systems such as Salesforce.
Syntax 1
The syntax if you already have your date as 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.
Syntax | {{FormatDateTime(Date, Format, Language)}} |
Input | date, text, text |
Output | DateTime formatted according to format and language |
|
Example 1
Use a date form field to insert text in a text binding that formats according to dddd d MMMM yyyy
for the en-US
language. The language setting is expressed in the output being Thursday
and November
.
Binding | {{FormatDateTime(Form.Date, "dddd d MMMM yyyy", "en-US")}} |
Input | Form.Date = 2020-11-12 |
Output | "Thursday 12 November 2020" |
Example 2
Use today's date that formats according to dddd d MMMM yyyy
for the nl-NL
language.
Binding | {{FormatDateTime(Now(), "dddd d MMMM yyyy", "nl-NL")}} |
Input | Today's date = 2020-11-12 |
Output | "donderdag 12 november 2020" |
Example 3
Use today's date that formats according to dddd d MMMM yyyy
for the DocumentLanguage set in the user profile.
Binding | {{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 to always 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 the FormatDateTime() function in combination with Switch() 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() function in combination with Now() 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 through 59.
- mm = minute, from 00 through 59.
- s = second, from 0 through 59.
- ss = second, from 0 through 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 |
|
Syntax 2
The syntax if you need to convert text to a date object automatically in the background. 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.
Syntax | {{FormatDateTime(DateTimeText, ResultFormat, ResultLanguage, SourceFormat, SourceLanguage)}} |
Input | text, text, text, text, text |
Output | DateTime formatted according to format and region |
Example 1
Converting a date from a text form field 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 2
You can also use FormatDateTime() 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 host system (e.g. yyyy-MM-ddTHH:mm:ss
or yyyyMMdd
), you have to use the appropriate binding.
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" |
Comments
0 comments
Article is closed for comments.