BigQuery · Datetime function
BigQuery FORMAT_DATETIME
FORMAT_DATETIME(format_string, datetime) turns a DATETIME into a STRING using % elements, for example FORMAT_DATETIME('%d/%m/%Y %H:%M', dt) gives 23/11/2026 18:45. Use it for display labels and file names.
FORMAT_DATETIME prints a DATETIME with the same format elements as FORMAT_DATE plus the time elements: %H hour (00-23), %I hour (01-12), %M minute, %S second, %p AM/PM, %T (same as %H:%M:%S), %R (%H:%M), %E3S seconds with milliseconds, %E*S seconds with all fractional digits, and %c for a full English representation.
A DATETIME has no time zone, so zone elements have nothing to print: %Z and %z come out as literal text. To print a local time with its zone or offset, keep the value as a TIMESTAMP and use FORMAT_TIMESTAMP with a time zone argument.
For the default layout, CAST(dt AS STRING) returns 2026-11-23 18:45:30 with a space, not the T separator the console shows.
Syntax
FORMAT_DATETIME(format_string, datetime_expr)Arguments and return value
| Argument | Description |
|---|---|
format_string | A STRING of format elements and literal text. |
datetime_expr | The DATETIME to format. NULL returns NULL. |
Returns: STRING
Examples
Common layouts
SELECT
FORMAT_DATETIME('%Y-%m-%d %H:%M', DATETIME '2026-11-23 18:45:30') AS iso_minutes,
FORMAT_DATETIME('%d/%m/%Y %I:%M %p', DATETIME '2026-11-23 18:45:30') AS twelve_hour,
FORMAT_DATETIME('%A %H:%M', DATETIME '2026-11-23 18:45:30') AS weekday_time,
FORMAT_DATETIME('%c', DATETIME '2026-11-23 18:45:30') AS full_text;| iso_minutes | twelve_hour | weekday_time | full_text |
|---|---|---|---|
| 2026-11-23 18:45 | 23/11/2026 06:45 PM | Monday 18:45 | Mon Nov 23 18:45:30 2026 |
Fractional seconds
SELECT
FORMAT_DATETIME('%H:%M:%E3S', DATETIME '2026-11-23 18:45:30.123456') AS millis,
FORMAT_DATETIME('%H:%M:%E*S', DATETIME '2026-11-23 18:45:30.123456') AS all_digits,
FORMAT_DATETIME('%H:%M:%S', DATETIME '2026-11-23 18:45:30.123456') AS whole_seconds;| millis | all_digits | whole_seconds |
|---|---|---|
| 18:45:30.123 | 18:45:30.123456 | 18:45:30 |
Zone elements print as text on a DATETIME
SELECT
FORMAT_DATETIME('%H:%M %Z', DATETIME '2026-11-23 18:45:00') AS datetime_zone,
FORMAT_TIMESTAMP('%H:%M %Z', TIMESTAMP '2026-11-23 18:45:00+00', 'Europe/Berlin') AS timestamp_zone;| datetime_zone | timestamp_zone |
|---|---|
| 18:45 %Z | 19:45 UTC+1 |
Hourly labels for a local-time report
WITH visits AS (
SELECT DATETIME '2026-11-23 08:15:00' AS local_time UNION ALL
SELECT DATETIME '2026-11-23 08:40:00' UNION ALL
SELECT DATETIME '2026-11-23 13:05:00'
),
hourly AS (
SELECT DATETIME_TRUNC(local_time, HOUR) AS hour, COUNT(*) AS visits
FROM visits
GROUP BY hour
)
SELECT FORMAT_DATETIME('%a %H:00', hour) AS label, visits
FROM hourly
ORDER BY hour;| label | visits |
|---|---|
| Mon 08:00 | 2 |
| Mon 13:00 | 1 |
Common mistakes
- Confusing
%M(minutes) with%m(month), and%I(12-hour clock) with%H(24-hour clock). - Expecting
%Zto print a zone. DATETIME has none; format a TIMESTAMP with a zone argument instead. - Porting
TO_CHAR(dt, 'YYYY-MM-DD HH24:MI')or MySQLDATE_FORMAT. Use % elements, orCAST(dt AS STRING FORMAT 'YYYY-MM-DD HH24:MI'). - Sorting by the formatted label. 'Mon 13:00' and 'Tue 08:00' sort alphabetically; order by the DATETIME.
FAQ
How do I convert a datetime to a string in BigQuery?
CAST(dt AS STRING) returns 2026-11-23 18:45:30. For other layouts use FORMAT_DATETIME, for example FORMAT_DATETIME('%d.%m.%Y %H:%M', dt).
How do I show AM and PM?
Use %I for the 12-hour clock and %p for AM or PM: FORMAT_DATETIME('%I:%M %p', dt). %P prints lowercase am or pm.
Related
Errors you may hit
More datetime functions
All BigQuery datetime functions · All functions · BigQuery error messages
