⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lang_datefunc.html

📁 sqlite的帮助文档
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>SQLite Query Language: Date And Time Functions</title><style type="text/css">body {    margin: auto;    font-family: "Verdana" "sans-serif";    padding: 8px 1%;}a { color: #45735f }a:visited { color: #734559 }.logo { position:absolute; margin:3px; }.tagline {  float:right;  text-align:right;  font-style:italic;  width:240px;  margin:12px;  margin-top:58px;}.toolbar {  font-variant: small-caps;  text-align: center;  line-height: 1.6em;  margin: 0;  padding:1px 8px;}.toolbar a { color: white; text-decoration: none; padding: 6px 12px; }.toolbar a:visited { color: white; }.toolbar a:hover { color: #80a796; background: white; }.content    { margin: 5%; }.content dt { font-weight:bold; }.content dd { margin-bottom: 25px; margin-left:20%; }.content ul { padding:0px; padding-left: 15px; margin:0px; }/* rounded corners */.se  { background: url(images/se.png) 100% 100% no-repeat #80a796}.sw  { background: url(images/sw.png) 0% 100% no-repeat }.ne  { background: url(images/ne.png) 100% 0% no-repeat }.nw  { background: url(images/nw.png) 0% 0% no-repeat }</style><meta http-equiv="content-type" content="text/html; charset=UTF-8">  </head><body><div><!-- container div to satisfy validator --><a href="index.html"><img class="logo" src="images/SQLite.gif" alt="SQLite Logo" border="0"></a><div><!-- IE hack to prevent disappearing logo--></div><div class="tagline">Small. Fast. Reliable.<br>Choose any three.</div><table width=100% style="clear:both"><tr><td>  <div class="se"><div class="sw"><div class="ne"><div class="nw">  <div class="toolbar">    <a href="about.html">About</a>    <a href="sitemap.html">Sitemap</a>    <a href="docs.html">Documentation</a>    <a href="download.html">Download</a>    <a href="copyright.html">License</a>    <a href="news.html">News</a>    <a href="http://www.sqlite.org/cvstrac/index">Developers</a>    <a href="support.html">Support</a>  </div></div></div></div></div></td></tr></table>  <a href="lang.html"><h2>SQL As Understood By SQLite</h2></a><h3>Date And Time Functions</h3><p>SQLite supports five date and time functions as follows:</p><p><ol><li> <b>date(</b><i>timestring, modifier, modifier, ...</i><b>)</b> </li><li> <b>time(</b><i>timestring, modifier, modifier, ...</i><b>)</b> </li><li> <b>datetime(</b><i>timestring, modifier, modifier, ...</i><b>)</b> </li><li> <b>julianday(</b><i>timestring, modifier, modifier, ...</i><b>)</b> </li><li> <b>strftime(</b><i>format, timestring, modifier, modifier, ...</i><b>)</b> </li></ol><p>All five functions take a time string as an argument. The time stringis followed by zero or more modifiers. The strftime() function also takes a format string as its first argument.</p><p>The date() function returns the date in this format: YYYY-MM-DD. The time() function returns the time as HH:MM:SS. The datetime() function returns "YYYY-MM-DD HH:MM:SS". The julianday() function returns the <a href="http://en.wikipedia.org/wiki/Julian_day">Julian day</a> - number of days since noon in Greenwich on November 24, 4714 B.C. (<a href="http://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar">Proleptic Gregorian calendar</a>). The strftime() routine returns the date formatted according to the format string specified as the first argument.The format string supports the most common substitutions found in the <a href="http://opengroup.org/onlinepubs/007908799/xsh/strftime.html">strftime() function</a> from the standard C library plus two new substitutions, %f and %J.The following is a complete list of valid strftime() substitutions:</p><blockquote><table border="0" cellpadding="0" cellspacing="0"><tr><td><td width="10"><td></tr><tr><td> %d <td><td> day of month: 00<tr><td> %f <td><td> fractional seconds: SS.SSS<tr><td> %H <td><td> hour: 00-24 <tr><td> %j <td><td> day of year: 001-366<tr><td> %J <td><td> Julian day number<tr><td> %m <td><td> month: 01-12<tr><td> %M <td><td> minute: 00-59<tr><td> %s <td><td> seconds since 1970-01-01<tr><td> %S <td><td> seconds: 00-59<tr><td> %w <td><td> day of week 0-6 with sunday==0<tr><td> %W <td><td> week of year: 00-53<tr><td> %Y <td><td> year: 0000-9999<tr><td> %% <td><td> %</table></blockquote><p>Notice that all other date and time functions can be expressedin terms of strftime():</p><blockquote><table border="0" cellpadding="0" cellspacing="0"><tr><td><b>Function</b><td width="30"><td><b>Equivalent strftime()</b><tr><td>   date(...)      <td><td>  strftime("%Y-%m-%d", ...)<tr><td>   time(...)      <td><td>  strftime("%H:%M:%S", ...)<tr><td>   datetime(...)  <td><td>  strftime("%Y-%m-%d %H:%M:%S", ...)<tr><td>   julianday(...) <td><td>  strftime("%J", ...)</table></blockquote><p>The only reasons for providing functions other than strftime() isfor convenience and for efficiency.</p><h3>Time Strings</h3><p>A time string can be in any of the following formats:</p><ol><li> <i>YYYY-MM-DD</i><li> <i>YYYY-MM-DD HH:MM</i><li> <i>YYYY-MM-DD HH:MM:SS</i><li> <i>YYYY-MM-DD HH:MM:SS.SSS</i><li> <i>YYYY-MM-DD</i><b>T</b><i>HH:MM</i><li> <i>YYYY-MM-DD</i><b>T</b><i>HH:MM:SS</i><li> <i>YYYY-MM-DD</i><b>T</b><i>HH:MM:SS.SSS</i><li> <i>HH:MM</i><li> <i>HH:MM:SS</i><li> <i>HH:MM:SS.SSS</i><li> <b>now</b><li> <i>DDDDDDDDDD</i></ol><p>In formats 5 through 7, the "T" is a literal character separating the date and the time, as required by <a href="http://www.w3c.org/TR/NOTE-datetime">ISO-8601</a>. Formats 8 through 10 that specify only a time assume a date of 2000-01-01. Format 11, the string 'now', is converted into the current date and time as obtained from the xCurrentTime methodof the <a href="c3ref/vfs.html">sqlite3_vfs</a> object in use.<a href="http://en.wikipedia.org/wiki/Coordinated_Universal_Time">Universal Coordinated Time (UTC)</a> is used. Format 12 is the <a href="http://en.wikipedia.org/wiki/Julian_day">Julian day number</a>expressed as a floating point value.</p><h3>Modifiers</h3><p>The time string can be followed by zero or more modifiers that alter the date and time string.  Each modifieris a transformation that is applied to the time string to its left.Modifiers are applied from left to right; order is important.The available modifiers are as follows.</p><ol><li> NNN days<li> NNN hours<li> NNN minutes<li> NNN.NNNN seconds<li> NNN months<li> NNN years<li> start of month<li> start of year<li> start of day<li> weekday N<li> unixepoch<li> localtime<li> utc </ol><p>The first six modifiers (1 through 6) simply add the specified amount of time to the date specified by the preceding timestring.Note that "&plusmn;NNN months" works by rendering the original date intothe YYYY-MM-DD format, adding the &plusmn;NNN to the MM month value, thennormalizing the result.  Thus, for example, the data 2001-03-31 modifiedby '+1 month' initially yields 2001-04-31, but April only has 30 daysso the date is normalized to 2001-05-01.  A similar effect occurs whenthe original date is February 29 of a leapyear and the modifier is&plusmn;N years where N is not a multiple of four.</p><p>The "start of" modifiers (7 through 9) shift the date backwards to the beginning of the current month, year or day.</p><p>The "weekday" modifier advances the date forward to the next date where the weekday number is N. Sunday is 0, Monday is 1, and so forth.</p><p>The "unixepoch" modifier (11) only works if it immediately follows a timestring in the DDDDDDDDDD format. This modifier causes the DDDDDDDDDD to be interpreted not as a Julian day number as it normally would be, but as<a href="http://en.wikipedia.org/wiki/Unix_time">Unix Time</a> - the number of seconds since 1970.  If the "unixepoch" modifier does notfollow a timestring of the form DDDDDDDDDD which expresses the numberof seconds since 1970 or if other modifiersseparate the "unixepoch" modifier from prior DDDDDDDDDD then thebehavior is undefined.</p><p>The "localtime" modifier (12) assumes the time string to its left is inUniversal Coordinated Time (UTC) and adjusts the timestring so that it displays localtime.  If "localtime"follows a time that is not UTC, then the behavior is undefined.The "utc" is the opposite of "localtime".  "utc" assumes that the stringto its left is in the local timezone and adjusts that string to be in UTC.If the prior string is not in localtime, then the result of "utc" isundefined.</p><h3>Examples</h3><p>Compute the current date.<p><blockquote>SELECT date('now');</blockquote><p>Compute the last day of the current month.</p><blockquote>SELECT date('now','start of month','+1 month','-1 day');</blockquote><p>Compute the date and time given a unix timestamp 1092941466.</p><blockquote>    SELECT datetime(1092941466, 'unixepoch');</blockquote><p>Compute the date and time given a unix timestamp 1092941466, and compensate for your local timezone.</p><blockquote>  SELECT datetime(1092941466, 'unixepoch', 'localtime');</blockquote><p>Compute the current unix timestamp.</p><blockquote>  SELECT strftime('%s','now');</blockquote><p>Compute the number of days since the signing of the US Declarationof Independent.</p><blockquote>  SELECT julianday('now') - julianday('1776-07-04');</blockquote><p>Compute the number of seconds since a particular moment in 2004:</p><blockquote>  SELECT strftime('%s','now') - strftime('%s','2004-01-01 02:34:56');</blockquote><p>Compute the date of the first Tuesday in Octoberfor the current year.</p><blockquote>  SELECT date('now','start of year','+9 months','weekday 2');</blockquote><p>Compute the time since the unix epoch in seconds (like strftime('%s','now') except includes fractional part):</p><blockquote>  SELECT (julianday('now') - 2440587.5)*86400.0;</blockquote><h3>Caveats And Bugs</h3><p>The computation of local time depends heavily on the whim of politicians and is thus difficult to get correct for all locales. In this implementation, the standard C library function localtime_r() is used to assist in the calculation of local time.  The localtime_r() C function normally only works for yearsbetween 1970 and 2037. For dates outside this range, SQLite attempts to map the year into an equivalent year within this range, do the calculation, then map the year back.</p><p>Date computations do not give correct results for dates before Julian day number 0 (-4713-11-24 12:00:00).</p><p>Non-Vista Windows platforms only support one set of DST rules. Vista only supports two. Therefore, on these platforms, historical DST calculations will be incorrect. For example, in the US, in 2007 the DST rules changed. Non-Vista Windows platforms apply the new 2007 DST rules to all previous years as well. Vista does somewhat bettergetting results correct back to 1986, when the rules were also changed.</p><p>All internal computations assume the <a href="http://en.wikipedia.org/wiki/Gregorian_calendar">Gregorian calendar</a> system.  It is also assumed that everyday is exactly 86400 seconds in duration.</p><hr><small><i>This page last modified 2008/11/12 14:57:19 UTC</i></small></div></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -