SQLite Date & Time - julianday()
In SQLite, you can work with date and time values using various built-in functions. One such function is the julianday()
function, which returns the Julian day number for a specifc date and time stamp.
Syntax
The syntax for the julianday()
function is as follows:
julianday(date_string, [modifier_1, modifier_2, ...])
date_string
: The date and time string to be converted to a Julian day number. This string can be in any format supported by SQLite, such asYYYY-MM-DD HH:MM:SS
.modifier_1, modifier_2, ...
: An optional list of modifiers to adjust the returned Julian day number. These modifiers can includelocaltime
,utc
, and others.
Example
Let's say we want to calculate the Julian day number for the date '2022-01-01'
. We can use the julianday()
function as follows:
SELECT julianday('2022-01-01');
Output
The output of the above example would be:
2459592.5
Explanation
In the example above, we use the julianday()
function to calculate the Julian day number for the date '2022-01-01'
. The function returns the Julian day number as a floating point value, which represents the number of days since noon GMT on January 1, 4713 BCE.
Use
The julianday()
function can be used to calculate the difference between two dates in days, or to compare dates using their Julian day numbers. It can also be used to perform date arithmetic and to manipulate dates in various other ways.
Important Points
- The
julianday()
function assumes the Gregorian calendar for dates before October 15, 1582, and the Julian calendar for earlier dates. - The
julianday()
function returns the Julian day number as a floating point value. To convert it to an integer value, you can use theCAST()
function, as follows:
SELECT CAST(julianday('2022-01-01') AS INTEGER);
- The
julianday()
function does not handle time zones. To convert a local time to UTC before calculating the Julian day number, use thedatetime()
function with theutc
modifier, as follows:
SELECT julianday(datetime('now', 'utc'));
Summary
In this tutorial, we learned about the julianday()
function in SQLite, used to calculate the Julian day number for a given date and time stamp. We saw examples of how to use the function, and discussed its uses and important points. The julianday()
function is useful for performing date arithmetic and for manipulating dates in various other ways.