SQLite 教程
1. SQLite 教程 2. SQLite 简介 3. SQLite 安装 4. SQLite 命令 5. SQLite 语法 6. SQLite 数据类型 7. SQLite 创建数据库 8. SQLite 附加数据库 9. SQLite 分离数据库 10. SQLite 创建表 11. SQLite 删除表 12. SQLite Insert 语句 13. SQLite Select 语句 14. SQLite 运算符 15. SQLite 表达式 16. SQLite Where 子句 17. SQLite AND/OR 运算符 18. SQLite Update 语句 19. SQLite Delete 语句 20. SQLite Like 子句 21. SQLite Glob 子句 22. SQLite Limit 子句 23. SQLite Order By 24. SQLite Group By 25. SQLite Having 子句 26. SQLite Distinct 关键字 27. SQLite PRAGMA 28. SQLite 约束 29. SQLite Join 30. SQLite Unions 子句 31. SQLite NULL 值 32. SQLite 别名 33. SQLite 触发器 34. SQLite 索引 35. SQLite Indexed By 36. SQLite Alter 命令 37. SQLite Truncate Table 38. SQLite 视图 39. SQLite 事务 40. SQLite 子查询 41. SQLite Autoincrement 42. SQLite 注入 43. SQLite Explain 44. SQLite Vacuum 45. SQLite 日期 & 时间 46. SQLite 常用函数 47. SQLite – C/C++ 48. SQLite – Java 49. SQLite – PHP 50. SQLite – Perl 51. SQLite – Python

SQLite 日期 & 时间

SQLite 日期 & 时间

SQLite 支持以下五个日期和时间函数:

序号函数实例
1date(timestring, modifier, modifier, ...)以 YYYY-MM-DD 格式返回日期。
2time(timestring, modifier, modifier, ...)以 HH:MM:SS 格式返回时间。
3datetime(timestring, modifier, modifier, ...)以 YYYY-MM-DD HH:MM:SS 格式返回。
4julianday(timestring, modifier, modifier, ...)这将返回从格林尼治时间的公元前 4714 年 11 月 24 日正午算起的天数。
5strftime(format, timestring, modifier, modifier, ...)这将根据第一个参数指定的格式字符串返回格式化的日期。具体格式见下边讲解。

上述五个日期和时间函数把时间字符串作为参数。时间字符串后跟零个或多个 modifier 修饰符。strftime() 函数也可以把格式字符串 format 作为其第一个参数。下面将为您详细讲解不同类型的时间字符串和修饰符。

时间字符串

一个时间字符串可以采用下面任何一种格式:

序号时间字符串实例
1YYYY-MM-DD2010-12-30
2YYYY-MM-DD HH:MM2010-12-30 12:10
3YYYY-MM-DD HH:MM:SS.SSS2010-12-30 12:10:04.100
4MM-DD-YYYY HH:MM30-12-2010 12:10
5HH:MM12:10
6YYYY-MM-DDTHH:MM2010-12-30 12:10
7HH:MM:SS12:10:01
8YYYYMMDD HHMMSS20101230 121001
9now2013-05-07

您可以使用 "T" 作为分隔日期和时间的文字字符。

修饰符(Modifier)

时间字符串后边可跟着零个或多个的修饰符,这将改变有上述五个函数返回的日期和/或时间。任何上述五大功能返回时间。修饰符应从左到右使用,下面列出了可在 SQLite 中使用的修饰符:

  • 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

格式化

SQLite 提供了非常方便的函数 strftime() 来格式化任何日期和时间。您可以使用以下的替换来格式化日期和时间:

替换描述
%d一月中的第几天,01-31
%f带小数部分的秒,SS.SSS
%H小时,00-23
%j一年中的第几天,001-366
%J儒略日数,DDDD.DDDD
%m月,00-12
%M分,00-59
%s从 1970-01-01 算起的秒数
%S秒,00-59
%w一周中的第几天,0-6 (0 is Sunday)
%W一年中的第几周,01-53
%Y年,YYYY
%%% symbol

实例

现在让我们使用 SQLite 提示符尝试不同的实例。下面是计算当前日期:


sqlite> SELECT date('now');

2013-05-07

下面是计算当前月份的最后一天:


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

2013-05-31

下面是计算给定 UNIX 时间戳 1092941466 的日期和时间:


sqlite> SELECT datetime(1092941466, 'unixepoch');

2004-08-19 18:51:06

下面是计算给定 UNIX 时间戳 1092941466 相对本地时区的日期和时间:


sqlite> SELECT datetime(1092941466, 'unixepoch', 'localtime');

2004-08-19 11:51:06

下面是计算当前的 UNIX 时间戳:


sqlite> SELECT strftime('%s','now');

1367926057

下面是计算美国"独立宣言"签署以来的天数:


sqlite> SELECT julianday('now') - julianday('1776-07-04');

86504.4775830326

下面是计算从 2004 年某一特定时刻以来的秒数:


sqlite> SELECT strftime('%s','now') - strftime('%s','2004-01-01 02:34:56');

295001572

下面是计算当年 10 月的第一个星期二的日期:


sqlite> SELECT date('now','start of year','+9 months','weekday 2');

2013-10-01

下面是计算从 UNIX 纪元算起的以秒为单位的时间(类似 strftime('%s','now') ,不同的是这里有包括小数部分):


sqlite> SELECT (julianday('now') - 2440587.5)*86400.0;

1367926077.12598

在 UTC 与本地时间值之间进行转换,当格式化日期时,使用 utc 或 localtime 修饰符,如下所示:


sqlite> SELECT time('12:00', 'localtime');

05:00:00


sqlite>  SELECT time('12:00', 'utc');

19:00:00