DateTime.ToText
Writes a datetime as text, in the culture's form or in a format you give.
Group: Dates and times. Power Query M has it too: a query that uses it is written as in Power BI.
Syntax
DateTime.ToText(dateTime as nullable datetime, optional options as any, optional culture as nullable text) as nullable text
| Parameter | Type | Description |
|---|---|---|
dateTime | nullable datetime | The datetime to write; null gives null. |
options | any optional | A record [Format = ..., Culture = ...], both fields optional; or, as older queries do, a format as text. |
culture | nullable text optional | A culture's name, when options is a format given as text. |
Returns
nullable text. The datetime as text.
Remarks
- Text follows the culture: the one given, else the query's (
Culture.Current), which is the model's Locale for import, else the model's own culture, else en-US. These examples run in en-US unless a culture is given. Left without a format, en-US writes 3/15/2024 2:30:00 PM; nb-NO writes 15.03.2024 14:30:00. - A format is built from letters:
yyyythe year,MMthe month as two digits,MMMandMMMMits short and full name,ddthe day,dddandddddthe weekday's short and full name,HHthe hour on a 24-hour clock,hhon a 12-hour clock withttfor AM or PM,mmminutes,ssseconds. Text in single quotes is copied as it is. - "o" gives the full ISO form with seven decimals of the second, good for exchanging data.
Examples
The culture's form
DateTime.ToText(#datetime(2024, 3, 15, 14, 30, 0))
"3/15/2024 2:30:00 PM"
A timestamp for a log
DateTime.ToText(#datetime(2024, 3, 15, 14, 30, 0), [Format = "yyyy-MM-dd HH:mm"])
"2024-03-15 14:30"
In Norwegian
DateTime.ToText(#datetime(2024, 3, 15, 14, 30, 0), [Culture = "nb-NO"])
"15.03.2024 14:30:00"
Related functions
DateTime.FromText, Date.ToText, Time.ToText, DateTimeZone.ToText.