Available Methods:
- date(time-value, modifier, modifier, ...)
- time(time-value, modifier, modifier, ...)
- datetime(time-value, modifier, modifier, ...)
- julianday(time-value, modifier, modifier, ...)
- strftime(format, time-value, modifier, modifier, ...)
strftime() substitutions:
Modifiers:
- NNN days
- NNN hours
- NNN minutes
- NNN.NNNN seconds
- NNN months
- NNN years
- start of month
- start of year
- start of day
- weekday N
- unixepoch
- localtime
- utc
Examples:
Compute the current date.
SELECT date('now');
// 2021-12-24
Compute the last day of the current month.
SELECT date('now','start of month','+1 month','-1 day');
Compute the date and time given a unix timestamp 1092941466.
SELECT datetime(1092941466, 'unixepoch');
Compute the date and time given a unix timestamp 1092941466, and compensate for your local timezone.
SELECT datetime(1092941466, 'unixepoch', 'localtime');
Compute the current unix timestamp.
SELECT strftime('%s','now');
Compute the number of days since the signing of the US Declaration of Independence.
SELECT julianday('now') - julianday('1776-07-04');
Compute the number of seconds since a particular moment in 2004:
SELECT strftime('%s','now') - strftime('%s','2004-01-01 02:34:56');
Compute the date of the first Tuesday in October for the current year.
SELECT date('now','start of year','+9 months','weekday 2');
Compute the time since the unix epoch in seconds (like strftime('%s','now') except includes fractional part):
SELECT (julianday('now') - 2440587.5)*86400.0;
More Practical Example With Employee Table
CREATE TABLE Employee
(eid INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
joining_date TEXT,
salary REAL);
INSERT INTO Employee(ename,joining_date,salary)
VALUES ('Rati',date('now','start of month'),5200),
('Umang',date('now','start of year'),5700),
('Prachi',date('now','start of year','-7 months'),5500),
('Ashmite',date('now','start of year','-32 days'),7000);
SELECT * FROM Employee;
eid ename joining_date salary
---------- ---------- ------------ ----------
1 Rati 2016-08-01 5200.0
2 Umang 2016-01-01 5700.0
3 Prachi 2015-06-01 5500.0
4 Ashmite 2015-11-30 7000.0
// add 6 month from joining
SELECT eid, ename, joining_date, salary, date(joining_date,'6 months') as 'Appraisal date' FROM Employee;
eid ename joining_date salary Appraisal date
---------- ---------- ------------ ---------- --------------
1 Rati 2016-08-01 5200.0 2017-02-01
2 Umang 2016-01-01 5700.0 2016-07-01
3 Prachi 2015-06-01 5500.0 2015-12-01
4 Ashmite 2015-11-30 7000.0 2016-05-30
Get the employee Data Joined in last 30 days
SELECT eid, ename, joining_date, salary FROM Employee
WHERE joining_date BETWEEN date('now', '-30 days') AND date('now');
eid ename joining_date salary
1 Rati 2021-12-01 5200.0
Comments
Post a Comment