Skip to content

formatSqlDateTime

formatSqlDateTime(value: string): string

Format a plain ISO 8601 datetime string as a SQL timestamp literal without a time zone — the YYYY-MM-DD HH:MM:SS[.fff] form of standard SQL TIMESTAMP and MySQL/SQLite DATETIME columns.

  • Grammar: SQL-92 §5.3 <timestamp string> and the ODBC ts escape, which parseSqlDateTime reads: the date, a single space, and the time with seconds always written.
  • A year outside SQL-92’s 0001–9999 (Table 10) returns "": the literal has no sign and no expanded year to write it with.
  • Plain only. SQL’s offset-carrying TIMESTAMP WITH TIME ZONE literal (YYYY-MM-DD HH:MM:SS±HH:MM, e.g. PostgreSQL) is out of scope — this targets the far more common tz-less column shape. There is no zoned counterpart.
  • Fractional seconds are written as Temporal writes them: only the digits needed, with trailing zeros dropped (.500 → .5).
ParameterTypeDescription
valuestringplain ISO 8601 datetime string (e.g. “2024-03-15T14:30:00”)

SQL timestamp literal, or "" on invalid input

import { formatSqlDateTime } from "@northguild/gmt/plain/format";
formatSqlDateTime("2024-03-15T14:30:00") // "2024-03-15 14:30:00"
output 

plain/format/formatSqlDateTime.ts