Sling Academy
Home/SQLite/SQLite Date and Time Functions Explained with Examples

SQLite Date and Time Functions Explained with Examples

Last updated: December 08, 2024

When working with databases, handling dates and times efficiently is crucial, especially for time-sensitive applications. SQLite offers robust support for date and time manipulation through a comprehensive set of built-in functions. This article will delve into the various date and time functions available in SQLite, illustrating their use with practical examples.

Understanding SQLite Date and Time Functions

SQLite supports five date and time functions:

  • date(timestring, modifier,...)
  • time(timestring, modifier,...)
  • datetime(timestring, modifier,...)
  • julianday(timestring, modifier,...)
  • strftime(format, timestring, modifier,...)

Before jumping into each function, it's important to understand the concept of the timestring. It's a string that represents a date and/or time. The common formats SQLite can recognize include:

  • 'YYYY-MM-DD' for date
  • 'HH:MM' or 'HH:MM:SS' for time
  • 'YYYY-MM-DD HH:MM:SS' for date and time
  • 'now' or 'localtime' or 'utc' for current time

Using the Date Function

The date() function returns the date in the 'YYYY-MM-DD' format. Here’s a simple example:

SELECT date('now');

This will return the current date.

You can also adjust dates using modifiers:

SELECT date('now', '+1 day');

This command adds one day to the current date.

Utilizing the Time Function

The time() function extracts or manipulates the time component of a date-time value:

SELECT time('now');

This example retrieves the current time.

DateTime Function

The datetime() function returns the current date and time in 'YYYY-MM-DD HH:MM:SS' format, and allows for modifications:

SELECT datetime('now', 'localtime');

This example shows how to adjust UTC to local time.

JulianDay Function

The julianday() function is used to work with the Julian day number for a timestamps:

SELECT julianday('now');

This query returns the Julian day number for the current moment.

Strftime Function

The strftime() function is the most flexible, translating date and time values into customized string formats:

SELECT strftime('%H:%M:%S', 'now');

This example extracts the time in 'HH:MM:SS' format from the current timestamp.

Modifiers

Modifiers help adjust the initial time or date value. Some commonly used modifiers include:

  • 'N days': Add or subtract N days
  • 'N months': Add or subtract N months
  • 'N years': Add or subtract N years
  • 'N hours': Add or subtract N hours
  • 'N minutes': Add or subtract N minutes
  • 'N seconds': Add or subtract N seconds

Practical Use Cases

Imagine you want to find all records in a database where a timestamp column indicates an event occurred within the last month:

SELECT * FROM events WHERE date(timestamp) >= date('now', '-1 month');

This statement retrieves all events happening in the last month.

Error handling is important, always check that your timestrings are valid and in the expected format to avoid unexpected results.

Using these SQLite date and time functions efficiently can save you significant effort and help ensure your application processes temporal data consistently and accurately. Experiment with these functions to support the full range of date and time needs for your projects.

Next Article: Using Mathematical Functions in SQLite for Complex Queries

Previous Article: String Functions in SQLite: From Basics to Advanced Use

Series: SQLite Functions and Extensions

SQLite

You May Also Like

  • How to use regular expressions (regex) in SQLite
  • SQLite UPSERT tutorial (insert if not exist, update if exist)
  • What is the max size allowed for an SQLite database?
  • SQLite Error: Invalid Value for PRAGMA Configuration
  • SQLite Error: Failed to Load Extension Module
  • SQLite Error: Data Type Mismatch in INSERT Statement
  • SQLite Warning: Query Execution Took Longer Than Expected
  • SQLite Error: Cannot Execute VACUUM on Corrupted Database
  • SQLite Error: Missing Required Index for Query Execution
  • SQLite Error: FTS5 Extension Malfunction Detected
  • SQLite Error: R-Tree Node Size Exceeds Limit
  • SQLite Error: Session Extension: Invalid Changeset Detected
  • SQLite Error: Invalid Use of EXPLAIN Statement
  • SQLite Warning: Database Connection Not Closed Properly
  • SQLite Error: Cannot Attach a Database in Encrypted Mode
  • SQLite Error: Insufficient Privileges for Operation
  • SQLite Error: Cannot Bind Value to Parameter
  • SQLite Error: Maximum String or Blob Size Exceeded
  • SQLite Error: Circular Reference in Foreign Key Constraints