Request a demo

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.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

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

ArgumentDescription
format_stringA STRING of format elements and literal text.
datetime_exprThe 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;
Result
iso_minutestwelve_hourweekday_timefull_text
2026-11-23 18:4523/11/2026 06:45 PMMonday 18:45Mon 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;
Result
millisall_digitswhole_seconds
18:45:30.12318:45:30.12345618: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;
Result
datetime_zonetimestamp_zone
18:45 %Z19: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;
Result
labelvisits
Mon 08:002
Mon 13:001

Common mistakes

  • Confusing %M (minutes) with %m (month), and %I (12-hour clock) with %H (24-hour clock).
  • Expecting %Z to print a zone. DATETIME has none; format a TIMESTAMP with a zone argument instead.
  • Porting TO_CHAR(dt, 'YYYY-MM-DD HH24:MI') or MySQL DATE_FORMAT. Use % elements, or CAST(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.

Errors you may hit

More datetime functions

All BigQuery datetime functions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.