Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

SQLite Tutorial

SQLite HOME SQLite Intro SQLite Get Started

SQLite SQL

SQLite SQL SQLite SELECT SQLite SELECT DISTINCT SQLite WHERE SQLite ORDER BY SQLite AND SQLite OR SQLite NOT SQLite INSERT INTO SQLite NULL Values SQLite UPDATE SQLite DELETE SQLite LIMIT SQLite Aggregate SQLite MIN() and MAX() SQLite COUNT() SQLite SUM() SQLite AVG() SQLite LIKE SQLite Wildcards SQLite GLOB SQLite IN SQLite BETWEEN SQLite Aliases SQLite Joins SQLite INNER JOIN SQLite LEFT JOIN SQLite RIGHT JOIN SQLite FULL JOIN SQLite CROSS JOIN SQLite Self Join SQLite UNION SQLite UNION ALL SQLite INTERSECT and EXCEPT SQLite GROUP BY SQLite HAVING SQLite EXISTS SQLite Subqueries SQLite INSERT INTO SELECT SQLite CASE SQLite NULL Functions SQLite Comments SQLite Operators

SQLite Advanced

SQLite UPSERT SQLite RETURNING SQLite WITH (CTE) SQLite Window Functions SQLite JSON

SQLite Database

SQLite Create DB SQLite Attach DB SQLite Create Table SQLite Drop Table SQLite Alter Table SQLite Data Types SQLite STRICT Tables SQLite Constraints SQLite NOT NULL SQLite UNIQUE SQLite PRIMARY KEY SQLite FOREIGN KEY SQLite CHECK SQLite DEFAULT SQLite AUTOINCREMENT SQLite Generated Columns SQLite Create Index SQLite Views SQLite Triggers SQLite Dates SQLite Transactions SQLite PRAGMA SQLite Injection

SQLite Tools

SQLite Command Line SQLite Import and Export SQLite with Python

SQLite References

SQLite Functions

SQLite Exercises

SQLite Editor SQLite Quiz SQLite Exercises

SQLite Dates


Share

No Date Type

SQLite has no special data type for dates. Dates are usually stored as text in the format YYYY-MM-DD, or YYYY-MM-DD HH:MM:SS with a time.

In this format, dates are sorted and compared in the right order. The Northwind database stores dates this way:

Below is a selection from the "Orders" table in the Northwind sample database:

OrderIDCustomerIDEmployeeIDOrderDateShipperID
102489051996-07-043
102498161996-07-051
102503441996-07-082

Date and Time Functions

FunctionReturns
date()The date, as YYYY-MM-DD
time()The time, as HH:MM:SS
datetime()The date and time, as YYYY-MM-DD HH:MM:SS
julianday()The number of days since a day far back in history, as a decimal number
unixepoch()The number of seconds since 1970-01-01
strftime()The date, formatted as you want


The Current Date and Time

The value 'now' means the current date and time, in UTC:

Example

SELECT date('now'), time('now'), datetime('now');
Try it Yourself »

Add the modifier 'localtime' to get the local time of your computer: datetime('now', 'localtime').


Calculate With Dates

Modifiers add or subtract time, or move to the start of a period:

Example

SELECT date('1996-07-04', '+7 days'),
  date('1996-07-04', '+1 month'),
  date('1996-07-04', 'start of month');
Try it Yourself »

Get a Part of a Date

strftime() formats a date. This example gets the year and the month of each order:

Example

SELECT OrderID, OrderDate,
  strftime('%Y', OrderDate) AS Year,
  strftime('%m', OrderDate) AS Month
FROM Orders;
Try it Yourself »

Orders per Month

Example

Count the orders in each month:

SELECT strftime('%Y-%m', OrderDate) AS Month,
  COUNT(*) AS Orders
FROM Orders
GROUP BY Month;
Try it Yourself »

The Number of Days Between Two Dates

Subtract two julianday() values to get the number of days between two dates:

Example

SELECT julianday('1997-02-12')
  - julianday('1996-07-04') AS Days;
Try it Yourself »

Format Codes

These are the most used codes for strftime():

CodeMeansExample
%YYear1996
%mMonth, 01 to 1207
%dDay of the month, 01 to 3104
%HHour, 00 to 2314
%MMinute, 00 to 5930
%SSecond, 00 to 5905
%wDay of the week, 0 to 6, where 0 is Sunday4
%jDay of the year, 001 to 366186



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.