发布于 2015-07-25 11:48:16 | 719 次阅读 | 评论: 0 | 来源: 网络整理

SQLite支持5个日期和时间函数如下:

S.N. 函数 例子
1 date(timestring, modifiers...) This returns the date in this format: YYYY-MM-DD
2 time(timestring, modifiers...) This returns the time as HH:MM:SS
3 datetime(timestring, modifiers...) This returns YYYY-MM-DD HH:MM:SS
4 julianday(timestring, modifiers...) This returns the number of days since noon in Greenwich on November 24, 4714 B.C.
5 strftime(timestring, modifiers...) This returns the date formatted according to the format string specified as the first argument formatted as per formatters explained below.

上述五个日期和时间函数时间字符串作为参数。后跟零个或多个修饰符的时间字符串。 strftime()函数还需要一个格式字符串作为其第一个参数。下面的部分将给予您详细的时间字符串和改性剂的不同类型。

时间字符串:

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

S.N. 时间字符串 例子
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”作为一个文字字符分隔日期和时间。

修饰符

随后的时间字符串可以由零个或多个的修饰符将改变日期和/或任何上述五大功能返回时间。修饰符应用于从左侧到右侧和下面的修饰符可在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 Day of month, 01-31
%f Fractional seconds, SS.SSS
%H Hour, 00-23
%j Day of year, 001-366
%J Julian day number, DDDD.DDDD
%m Month, 00-12
%M Minute, 00-59
%s Seconds since 1970-01-01
%S Seconds, 00-59
%w Day of week, 0-6 (0 is Sunday)
%W Week of year, 01-53
%Y Year, 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 datetime(1092941466, 'unixepoch', 'localtime');
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
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务