SQLite date() Function
The SQLite date()
function converts a time value specified by a time value and modifiers to a date string in YYYY-MM-DD
format.
Syntax
Here is the syntax of the SQLite date()
function:
date(time_value [, modifier, modifier, ...])
Parameters
time_value
-
Optional. A time value. The time value can be in any of the following formats, as shown below. The value is usually a string, but in the case of format 12 it can be an integer or a floating point number.
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
- the current date and timeDDDDDDDDDD.dddddd
- Julian days number with fractional part
modifier
-
Optional. You can use zero or more modifiers to change the time value
time_value
. Multiple modifiers are applied sequentially from left to right. You can use modifiers like this:NNN days
- AddNNN
days to the time valueNNN hours
- AddNNN
hours to the time valueNNN minutes
- AddNNN
minutes to the time valueNNN.NNNN seconds
- AddNNN.NNNN
seconds to the time valueNNN months
- AddNNN
months to the time valueNNN years
- AddNNN
years to the time valuestart of month
- Fall back to the beginning of the month intime_value
start of year
- Fall back to the beginning of the year intime_value
start of day
- Fall back to the beginning of the day intime_value
weekday N
- Advancetime_value
ββto weekdayN
unixepoch
- Unix timestamp for nowjulianday
- The Julian calendar days for nowauto
localtime
- The current timeutc
- The utc time
The
NNN
represents a number. Can be a positive or negative number. IfNNN
is negative, it means subtraction.
Return value
The SQLite date()
function returns a date string in YYYY-MM-DD
format. If no arguments are provided, the date()
function returns the current date.
Examples
Here are some examples to show usages of the SQLite date()
function.
-
Get the current date using the SQLite
date()
function:SELECT date();
date() ---------- 2022-07-26
Alternatively, you can use the SQLite
date()
function with a time value'now'
to get the current date:SELECT date('now');
date('now') ----------- 2022-07-26
-
Use the SQLite
date()
function to get the first day of the current year:SELECT date('now', 'start of year');
date('now', 'start of year') ---------------------------- 2022-01-01
-
Use the SQLite
date()
function to get the last day of the current year:SELECT date('now', 'start of year', '1 year', '-1 day');
date('now', 'start of year', '1 year', '-1 day') ------------------------------------------------ 2022-12-31
-
Use the SQLite
date()
function to get the current year’s Mother’s Day:SELECT date('now', 'start of year', '4 months', 'weekday 0', '7 days');
date('now', 'start of year', '4 months', 'weekday 0', '7 day ------------------------------------------------------------ 2022-05-08
We know that Mother’s Day is the second Sunday in May every year.
To get it, we use the time value
'now'
, and then use several modifiers:'start of year'
,'4 months'
,'weekday 0'
,'7 days'
. The following is the calculating stpes for thedate()
function:'now'
- Get current time'start of year'
- Fall back the current time to the first day of the current year. That is2022-01-01
.'4 months'
- Add 4 months from the first day of the year. That is2022-05-01
.'weekday 0'
- Get the first Sunday starting at2022-05-01
. That is2022-05-01
.'7 days'
- Add 7 days from the first Sunday, which is the second Sunday in May. The result is2022-05-08
.