Skip to content

isValidSqlDateTime

isValidSqlDateTime(value: string): boolean

Return true when value is a real SQL timestamp literal without a time zone: the YYYY-MM-DD HH:MM:SS[.fff] value of a standard SQL TIMESTAMP or a MySQL/SQLite DATETIME column. It checks one date-and-time value, not a SQL statement.

  • True exactly when parseSqlDateTime returns a plain string for the same value: the validator calls the parser, so the two cannot disagree.
  • Grammar: SQL-92 §5.3 <timestamp string> and the ODBC ts escape, as sqlDateTime matches: a single space separator, a four-digit year 0001–9999, two-digit month, day, hour, minute and second (seconds are required), and an optional . followed by up to 9 fraction digits.
  • Checks the calendar as well as the shape. The sqlDateTime pattern proves the shape alone, so it matches a day that does not exist: 30 February, 29 February in a common year and 31 June are false here.
  • False for a second of 60: GMT rejects leap seconds.
  • Plain only. A T separator, a Z and an offset are false: SQL’s offset-carrying TIMESTAMP WITH TIME ZONE literal is out of scope.
  • False for a non-string. Read the value with parseSqlDateTime; write one with formatSqlDateTime.
ParameterTypeDescription
valuestringcandidate SQL timestamp literal

boolean indicating validity

import { isValidSqlDateTime } from "@northguild/gmt/plain/validate";
isValidSqlDateTime("2024-03-15 14:30:00") // true
output 

plain/validate/isValidSqlDateTime.ts