SQLite Date function

The SQLite date() function is a built-in function that is used to retrieve the current date in a specified format. The function returns a string value that represents the date, and it can be used in various contexts, such as in queries or in programming.

Syntax

The syntax of the date() function is as follows:

date(timestring, modifier1, modifier2, ...)

The timestring parameter is required and specifies the input date string, which must be in one of the following formats:

YYYY-MM-DD
YYYY-MM-DD HH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS

The modifier parameters are optional and can be used to modify the output format of the date. For example, the +N days modifier can be used to add N days to the current date, or the -N days modifier can be used to subtract N days from the current date.

Examples

Here are some examples of using the date() function:

1. Retrieve the current date in the default format:

SELECT date('now');

This will return a string value in the format YYYY-MM-DD.

2. Retrieve the current date and time in a specific format:

SELECT date('now', 'localtime', '+2 hours', '+30 minutes');

This will return a string value in the format YYYY-MM-DD HH:MM:SS, adjusted for the local time zone and with an additional offset of 2 hours and 30 minutes.

3. Add or subtract days from the current date:

SELECT date('now', '+7 days');

This will return a string value that represents the date 7 days from the current date.

Overall, the date() function is a useful tool for retrieving and manipulating dates in SQLite, and it can be customized to suit specific needs and requirements.