Skip to main content

How to Get the Current Date and Time in SQLite with datetime('now')

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Notes, orders and log lines usually record when they were made. SQLite's datetime('now') returns the current date and time, and a column default can fill it in on every insert. This guide reads the clock and stamps a note with it, with real output from SQLite 3.54.1.

Syntax​

Syntax
SELECT datetime('now');
SELECT date('now');
SELECT time('now');
PartWhat it means
datetime('now')The current date and time, as YYYY-MM-DD HH:MM:SS
date('now')Only the date, as YYYY-MM-DD
time('now')Only the time, as HH:MM:SS

All three use UTC, not your computer's time zone. SQLite's documentation on date and time functions: "Format 11, the string 'now', is converted into the current date and time". It goes on: "Universal Coordinated Time (UTC) is used."

Prerequisites​

  • The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run sqlite3 --version to see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.

Step 1: Write the Script​

Save this as current_time.sql:

current_time.sql
SELECT datetime('now'), date('now'), time('now');

CREATE TABLE notes (
body TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
);

INSERT INTO notes (body) VALUES ('Water the plants');

SELECT * FROM notes;

The current_time.sql script in a macOS-style code window: a SELECT of datetime('now'), date('now') and time('now'), a notes table whose created_at column defaults to datetime('now') in parentheses, an insert that gives only the body, and a SELECT of every note.

  • The first SELECT reads the clock three ways.
  • created_at TEXT NOT NULL DEFAULT (datetime('now')) stamps each new note with the time it's inserted. A function call in a default has to be wrapped in parentheses.
  • The INSERT gives only the note's text.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
sqlite3 -table notes.db ".read current_time.sql"

sqlite3 opens the database file notes.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.

Output
+---------------------+-------------+-------------+
| datetime('now') | date('now') | time('now') |
+---------------------+-------------+-------------+
| 2026-12-03 09:36:27 | 2026-12-03 | 09:36:27 |
+---------------------+-------------+-------------+
+------------------+---------------------+
| body | created_at |
+------------------+---------------------+
| Water the plants | 2026-12-03 09:36:27 |
+------------------+---------------------+

The output in a terminal: datetime('now') as 2026-12-03 09:36:27, date('now') as 2026-12-03 and time('now') as 09:36:27, then the note Water the plants with created_at 2026-12-03 09:36:27.

The three functions returned the same moment in three forms, and the note got that time in created_at without its INSERT mentioning the column. Your run shows your own date and time, in UTC.

Common Mistakes​

MistakeWhat happensDo this instead
A default without parentheses, DEFAULT datetime('now')Parse error ...: near "(": syntax errorWrap the call: DEFAULT (datetime('now'))
Expecting your local timeThe times are UTC, so they can be hours off your clockStore UTC, and convert only to show it: datetime('now', 'localtime')

Troubleshooting​

SymptomLikely causeFix
The stored time is hours off'now' is UTCAdd 'localtime' when you display the time
Parse error near line 3 of current_time.sql: table notes already existsThe script already ran on notes.dbDelete notes.db and run the script again

FAQ​

Is CURRENT_TIMESTAMP the same as datetime('now')?​

It gives the same form. A column with DEFAULT CURRENT_TIMESTAMP stored 19 characters, equal to datetime('now'). CURRENT_TIMESTAMP is a keyword, so it needs no parentheses.

How do I convert to local time?​

With the 'localtime' modifier: datetime('now', 'localtime'). The documentation: "The "localtime" modifier assumes the time-value to its left is in Universal Coordinated Time (UTC) and adjusts that time value so that it is in localtime."

Does 'now' change during one statement?​

No. datetime('now') = datetime('now') gave 1. The documentation: "The 'now' argument to date and time functions always returns exactly the same value for multiple invocations within the same sqlite3_step() call."

Conclusion​

datetime('now') read the current UTC time, and DEFAULT (datetime('now')) stamped it on the new note without the INSERT asking.

Additional Resources​