SQLite Tutorial

  • Home
  • Start Here
  • Views
  • Indexes
  • Triggers
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
  • API
    • SQLite Python
    • SQLite Node.js
    • SQLite Java
    • SQLite PHP
  • Try It
Home / SQLite Tutorial / SQLite Date & Time

SQLite Date & Time

Summary: in this tutorial, we will show you how to work with the SQLite date and time values and use the built-in dates and times functions to handle date and time values.

SQLite does not support built-in date and/or time storage class. Instead, it leverages some built-in date and time functions to use other storage classes such as TEXT, REAL, or INTEGER for storing the date and time values.

Using the TEXT storage class for storing SQLite date and time

If you use the TEXT storage class to store date and time value, you need to use the ISO8601 string format as follows:

YYYY-MM-DD HH:MM:SS.SSS
Code language: SQL (Structured Query Language) (sql)

For example, 2016-01-01 10:20:05.123

First, create a new table named datetime_text for demonstration.

CREATE TABLE datetime_text( d1 text, d2 text );
Code language: SQL (Structured Query Language) (sql)

Try It

The table contains two column d1 and d2 with TEXT datatype.

To insert date and time values into the datetime_text table, you use the DATETIME function.

For example, to get the current UTC date and time value, you pass the now literal string to the function as follows:

SELECT datetime('now');
Code language: SQL (Structured Query Language) (sql)

Try It

To get the local time, you pass an additional argument  localtime.

SELECT datetime('now','localtime');
Code language: SQL (Structured Query Language) (sql)

Try It

Second, insert the date and time values into the datetime_text table as follows:

INSERT INTO datetime_text (d1, d2) VALUES(datetime('now'),datetime('now', 'localtime'));
Code language: SQL (Structured Query Language) (sql)

Try It

Third, query the data from the datetime_text table.

SELECT d1, typeof(d1), d2, typeof(d2) FROM datetime_text;
Code language: SQL (Structured Query Language) (sql)

Try It

SQLite Date Using TEXT data type

Using REAL storage class to store SQLite date and time values

You can use the REAL storage class to store the date and/ or time values as Julian day numbers, which is the number of days since noon in Greenwich on November 24, 4714 B.C. based on the proleptic Gregorian calendar.

Let’s take a look at an example of using the REAL storage class to store date and time values.

First, create a new table named datetime_real.

CREATE TABLE datetime_real( d1 real );
Code language: SQL (Structured Query Language) (sql)

Try It

Second, insert the “current” date and time value into the datetime_real table.

INSERT INTO datetime_real (d1) VALUES(julianday('now'));
Code language: SQL (Structured Query Language) (sql)

Try It

We used the  julianday() function to convert the current date and time to the Julian Day.

Third, query data from the datetime_real table.

SELECT d1 FROM datetime_real;
Code language: SQL (Structured Query Language) (sql)

Try It

SQLite Date Using REAL data type

The output is not human readable.

Fortunately, you can use the built-in date() and time() functions to format a date and time value as follows:

SELECT date(d1), time(d1) FROM datetime_real;
Code language: SQL (Structured Query Language) (sql)

Try It

SQLite Date and Time functions

Using INTEGER to store SQLite date and time values

Besides  TEXT and REAL storage classes, you can use the INTEGER storage class to store date and time values.

We typically use the INTEGER to store UNIX time which is the number of seconds since 1970-01-01 00:00:00 UTC. See the following example:

First, create a table that has one column whose data type is INTEGER to store the date and time values.

CREATE TABLE datetime_int (d1 int);
Code language: SQL (Structured Query Language) (sql)

Try It

Second, insert the current date and time value into the datetime_int table.

INSERT INTO datetime_int (d1) VALUES(strftime('%s','now'));
Code language: SQL (Structured Query Language) (sql)

Try It

Third, query data from the datetime_int table.

SELECT d1 FROM datetime_int;
Code language: SQL (Structured Query Language) (sql)

Try It

It’s an integer.

To format the result, you can use the built-in datetime() function as follows:

SELECT datetime(d1,'unixepoch') FROM datetime_int;
Code language: SQL (Structured Query Language) (sql)

Try It

SQlite DATETIME function

Using SQLite, you can freely choose any data types to store date and time values and use the built-in dates and times function to convert between formats.

For the detailed information on SQLite dates and times functions, check it out the built-in dates and times functions.

In this tutorial, you have learned how to use the TEXT, REAL, and INTEGER storage classes to store date and time values. In addition, you learned how to use the built-in dates and times functions to convert the stored date and times values into readable formats.

  • Was this tutorial helpful ?
  • YesNo
Previous SQLite Data Types
Next SQLite Attach Database

Getting Started

  • What Is SQLite
  • Download & Install SQLite
  • SQLite Sample Database
  • SQLite Commands

SQLite Tutorial

  • SQLite Select
  • SQLite Order By
  • SQLite Select Distinct
  • SQLite Where
  • SQLite Limit
  • SQLite BETWEEN
  • SQLite IN
  • SQLite Like
  • SQLite IS NULL
  • SQLite GLOB
  • SQLite Join
  • SQLite Inner Join
  • SQLite Left Join
  • SQLite Cross Join
  • SQLite Self-Join
  • SQLite Full Outer Join
  • SQLite Group By
  • SQLite Having
  • SQLite Union
  • SQLite Except
  • SQLite Intersect
  • SQLite Subquery
  • SQLite EXISTS
  • SQLite Case
  • SQLite Insert
  • SQLite Update
  • SQLite Delete
  • SQLite Replace
  • SQLite Transaction

SQLite Data Definition

  • SQLite Data Types
  • SQLite Date & Time
  • SQLite Create Table
  • SQLite Primary Key
  • SQLite Foreign Key
  • SQLite NOT NULL Constraint
  • SQLite UNIQUE Constraint
  • SQLite CHECK constraints
  • SQLite AUTOINCREMENT
  • SQLite Alter Table
  • SQLite Rename Column
  • SQLite Drop Table
  • SQLite Create View
  • SQLite Drop View
  • SQLite Index
  • SQLite Expression-based Index
  • SQLite Trigger
  • SQLite VACUUM
  • SQLite Transaction
  • SQLite Full-text Search

SQLite Tools

  • SQLite Commands
  • SQLite Show Tables
  • SQLite Describe Table
  • SQLite Dump
  • SQLite Import CSV
  • SQLite Export CSV

SQLite Functions

  • SQLite AVG
  • SQLite COUNT
  • SQLite MAX
  • SQLite MIN
  • SQLite SUM

SQLite Interfaces

  • SQLite PHP
  • SQLite Node.js
  • SQLite Java
  • SQLite Python

About SQLite Tutorial

SQLite Tutorial website helps you master SQLite quickly and easily. It explains the complex concepts in simple and easy-to-understand ways so that you can both understand SQLite fast and know how to apply it in your software development work more effectively.

Looking for a tutorial…

If you did not find the tutorial that you are looking for, you can use the following search box. In case the tutorial is not available, you can request for it using the request for a SQLite tutorial form.

Recent Tutorials

  • SQLite IIF
  • SQLite Generated Columns
  • SQLite Getting Started
  • SQLite Programming Interfaces
  • SQLite Concat
  • SQLite INSTEAD OF Triggers
  • SQLite Join
  • SQLite IS NULL

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2021 SQLite Tutorial. All Rights Reserved.