// dates & times reference

SQL SQLite date() function

Returns a formatted time string built from an input value and optional modifiers; SQLite stores dates as TEXT, REAL or INTEGER, never a DATE type.

類別
Dates & times
Returns
TEXT (ISO-8601)
In this category
29 of 121

語法

date('2024-01-15')
datetime('now', 'localtime')
datetime(1700000000, 'unixepoch')

Each block is a separate form. The support table and the examples show which engine takes which.

引擎支援

引擎Support備註
SQL Server Different name or syntax CAST(... AS date/time)
PostgreSQL Different name or syntax ts::date, ts::time
MySQL Different name or syntax DATE(), TIME(), FROM_UNIXTIME()
SQLite 是 Supported under this name.
Oracle Different name or syntax TRUNC(d) and TO_CHAR

範例

SQLite

SELECT date('now', 'start of month', '+1 month', '-1 day') AS month_end;

SQLite

SELECT datetime(1700000000, 'unixepoch') AS created;

SQLite

SELECT time('15:45') AS t;

小知識

  • The return is text in ISO-8601; comparisons work because of the format, but arithmetic needs julianday() or strftime().
  • 'now' is UTC: datetime('now') is hours off local time unless you append the 'localtime' modifier.
  • A bare number is a Julian day, not an epoch: datetime(1700000000) returns NULL until you add 'unixepoch'. SQLite 3.38+ also has unixepoch() for the reverse direction.

Questions about date() / time() / datetime()

What is the SQLite DATE type?

There is none. Values live as TEXT ('YYYY-MM-DD...'), REAL (Julian day) or INTEGER (Unix seconds), and these functions convert between the forms.

How do I add a month in SQLite?

date(d, '+1 month'). Unlike most engines it does not clamp: date('2024-01-31', '+1 month') is 2024-03-02, because February 31 normalizes forward. Recent versions accept a trailing 'floor' modifier that returns 2024-02-29 instead.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.