📜  SQLite日期

📅  最后修改于: 2020-11-13 00:40:41             🧑  作者: Mango

SQLite日期功能

SQLite的“ DATE”函数用于检索日期并以“ YYYY-MM-DD”格式返回。

句法:

date(timestring, [ modifier1, modifier2, ... modifier_n ] ) 

在这里,时间字符串是一个日期值,可以是以下任意一个:

Index timestring Description
1) now It is a literal used to return the current date.
2) YYYY-MM-DD It specifies the date value formatted as ‘YYYY-MM-DD’
3) YYYY-MM-DD HH:MM It specifies the date value formatted as ‘YYYY-MM-DD HH:MM’
4) YYYY-MM-DD HH:MM:SS It specifies the date value formatted as ‘YYYY-MM-DD HH:MM:SS’
5) YYYY-MM-DD HH:MM:SS.SSS It specifies the date value formatted as ‘YYYY-MM-DD HH:MM:SS.SSS’
6) HH:MM It specifies the date value formatted as ‘HH:MM’
7) HH:MM:SS It specifies the date value formatted as ‘HH:MM:SS’
8) HH:MM:SS.SSS It specifies the date value formatted as ‘HH:MM:SS.SSS’
9) YYYY-MM-DDTHH:MM It specifies the date value formatted as ‘YYYY-MM-DDTHH:MM’ where t is a literal character separating the date and time portions.
10) YYYY-MM-DDTHH:MM:SS It specifies the date value formatted as ‘YYYY-MM-DDTHH:MM:SS’ where t is a literal character separating the date and time portions
11) YYYY-MM-DDTHH:MM:SS.SSS It specifies the date value formatted as ‘YYYY-MM-DDTHH:MM:SS.SSS’ where t is a literal character separating the date and time portions
12) DDDDDDDDDD It specifies the Julian date number

修饰符1,修饰符2,…修饰符_n:修饰符是可选的。这些与时间字符串一起使用以增加或减少时间,日期或年份。

Index Modifier Description
1) [+-]NNN years It is used to specify number of years added/subtracted to the date
2) [+-]NNN months It is used to specify number of months added/subtracted to the date
3) [+-]NNN days It is used to specify number of days added/subtracted to the date
4) [+-]NNN hours It is used to specify number of hours added/subtracted to the date
5) [+-]NNN minutes It is used to specify number of minutes added/subtracted to the date
6) [+-]NNN seconds It is used to specify number of seconds added/subtracted to the date
7) [+-]NNN.NNNN seconds It is used to specify number of seconds (and fractional seconds) added/subtracted to the date
8) start of year It is used to shift the date back to the start of the year
9) start of month It is used to shift the date back to the start of the month
10) start of day It is used to shift the date back to the start of the day
11) weekday N It is used to move the date forward to the next date where weekday number is N
(0=Sunday, 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday)
12) unixepoch It is used with the DDDDDDDDDD timestring to interpret the date as UNIX Time (ie: number of seconds since 1970-01-01)
13) localtime It is used to adjust date to localtime, assuming the timestring was expressed in UTC
14) utc It is used to adjust date to utc, assuming the timestring was expressed in localtime

范例1:

检索当前日期:

SELECT date('now'); 

输出:

示例2:检索每月的第一天:

有四种方法可以找出每月的第一天:

SELECT date('2017-12-17', 'start of month');
SELECT date('now', 'start of month');
SELECT date('2014-10-16', '-15 days'); 
SELECT date('now', '-11 days');

输出:




范例3:撷取每月的最后一天:

日期函数可用于检索该月的最后一天。有四种方法可以找出当月的最后一天:

SELECT date('2017-04-13', 'start of month','+1 month', '-1 day');

SELECT date('now', 'start of month','+1 month', '-1 day');

SELECT date('2017-04-13', '+17 days');

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

输出:



例4:在当前日期加上/减去年份:

到当前日期加减5年:

SELECT date('now','+5 years');

SELECT date('2017-04-13','+5 years');

SELECT date('now','-5 years');

SELECT date('2017-04-13','-5 years'); 

输出:




示例5:在当前日期上添加/减去天数:

通过与上述相同的方法,您可以在日期中增加或减少天数:

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

SELECT date('2017-04-13','+5 days');

SELECT date('now','-5 days');

SELECT date('2017-04-13','-5 days'); 

输出: