SQLite 支持以下五个日期和时间函数:
序号 | 函数 | 实例 |
---|---|---|
1 | date(timestring, modifier, modifier, …) | 以 YYYY-MM-DD 格式返回日期。 |
2 | time(timestring, modifier, modifier, …) | 以 HH:MM:SS 格式返回时间。 |
3 | datetime(timestring, modifier, modifier, …) | 以 YYYY-MM-DD HH:MM:SS 格式返回。 |
4 | julianday(timestring, modifier, modifier, …) | 这将返回从格林尼治时间的公元前 4714 年 11 月 24 日正午算起的天数。 |
5 | strftime(format, timestring, modifier, modifier, …) | 这将根据第一个参数指定的格式字符串返回格式化的日期。具体格式见下边讲解。 |
上述五个日期和时间函数把时间字符串作为参数。时间字符串后跟零个或多个 modifier 修饰符。strftime() 函数也可以把格式字符串 format 作为其第一个参数。下面将为您详细讲解不同类型的时间字符串和修饰符。
时间字符串
一个时间字符串可以采用下面任何一种格式:
序号 | 时间字符串 | 实例 |
---|---|---|
1 | YYYY-MM-DD | 2010-12-30 |
2 | YYYY-MM-DD HH:MM | 2010-12-30 12:10 |
3 | YYYY-MM-DD HH:MM:SS.SSS | 2010-12-30 12:10:04.100 |
4 | MM-DD-YYYY HH:MM | 30-12-2010 12:10 |
5 | HH:MM | 12:10 |
6 | YYYY-MM-DDTHH:MM | 2010-12-30 12:10 |
7 | HH:MM:SS | 12:10:01 |
8 | YYYYMMDD HHMMSS | 20101230 121001 |
9 | now | 2013-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 提示符尝试不同的实例。下面是计算当前日期:
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT date</span><span class="pun">(</span><span class="str">'now'</span><span class="pun">);</span> <span class="lit">2013</span><span class="pun">-</span><span class="lit">05</span><span class="pun">-</span><span class="lit">07</span> |
下面是计算当前月份的最后一天:
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT date</span><span class="pun">(</span><span class="str">'now'</span><span class="pun">,</span><span class="str">'start of month'</span><span class="pun">,</span><span class="str">'+1 month'</span><span class="pun">,</span><span class="str">'-1 day'</span><span class="pun">);</span> <span class="lit">2013</span><span class="pun">-</span><span class="lit">05</span><span class="pun">-</span><span class="lit">31</span> |
下面是计算给定 UNIX 时间戳 1092941466 的日期和时间:
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT datetime</span><span class="pun">(</span><span class="lit">1092941466</span><span class="pun">,</span> <span class="str">'unixepoch'</span><span class="pun">);</span> <span class="lit">2004</span><span class="pun">-</span><span class="lit">08</span><span class="pun">-</span><span class="lit">19</span> <span class="lit">18</span><span class="pun">:</span><span class="lit">51</span><span class="pun">:</span><span class="lit">06</span> |
下面是计算给定 UNIX 时间戳 1092941466 相对本地时区的日期和时间:
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT datetime</span><span class="pun">(</span><span class="lit">1092941466</span><span class="pun">,</span> <span class="str">'unixepoch'</span><span class="pun">,</span> <span class="str">'localtime'</span><span class="pun">);</span> <span class="lit">2004</span><span class="pun">-</span><span class="lit">08</span><span class="pun">-</span><span class="lit">19</span> <span class="lit">11</span><span class="pun">:</span><span class="lit">51</span><span class="pun">:</span><span class="lit">06</span> |
下面是计算当前的 UNIX 时间戳:
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT strftime</span><span class="pun">(</span><span class="str">'%s'</span><span class="pun">,</span><span class="str">'now'</span><span class="pun">);</span> <span class="lit">1367926057</span> |
下面是计算美国"独立宣言"签署以来的天数:
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT julianday</span><span class="pun">(</span><span class="str">'now'</span><span class="pun">)</span> <span class="pun">-</span><span class="pln"> julianday</span><span class="pun">(</span><span class="str">'1776-07-04'</span><span class="pun">);</span> <span class="lit">86504.4775830326</span> |
下面是计算从 2004 年某一特定时刻以来的秒数:
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT strftime</span><span class="pun">(</span><span class="str">'%s'</span><span class="pun">,</span><span class="str">'now'</span><span class="pun">)</span> <span class="pun">-</span><span class="pln"> strftime</span><span class="pun">(</span><span class="str">'%s'</span><span class="pun">,</span><span class="str">'2004-01-01 02:34:56'</span><span class="pun">);</span> <span class="lit">295001572</span> |
下面是计算当年 10 月的第一个星期二的日期:
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT date</span><span class="pun">(</span><span class="str">'now'</span><span class="pun">,</span><span class="str">'start of year'</span><span class="pun">,</span><span class="str">'+9 months'</span><span class="pun">,</span><span class="str">'weekday 2'</span><span class="pun">);</span> <span class="lit">2013</span><span class="pun">-</span><span class="lit">10</span><span class="pun">-</span><span class="lit">01</span> |
下面是计算从 UNIX 纪元算起的以秒为单位的时间(类似 strftime('%s',’now') ,不同的是这里有包括小数部分):
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">(</span><span class="pln">julianday</span><span class="pun">(</span><span class="str">'now'</span><span class="pun">)</span> <span class="pun">-</span> <span class="lit">2440587.5</span><span class="pun">)*</span><span class="lit">86400.0</span><span class="pun">;</span> <span class="lit">1367926077.12598</span> |
在 UTC 与本地时间值之间进行转换,当格式化日期时,使用 utc 或 localtime 修饰符,如下所示:
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT time</span><span class="pun">(</span><span class="str">'12:00'</span><span class="pun">,</span> <span class="str">'localtime'</span><span class="pun">);</span> <span class="lit">05</span><span class="pun">:</span><span class="lit">00</span><span class="pun">:</span><span class="lit">00</span> |
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT time</span><span class="pun">(</span><span class="str">'12:00'</span><span class="pun">,</span> <span class="str">'utc'</span><span class="pun">);</span> <span class="lit">19</span><span class="pun">:</span><span class="lit">00</span><span class="pun">:</span><span class="lit">00</span> |