SQLite時間函式&日期時間、字串、時間戳互相轉換
阿新 • • 發佈:2019-01-07
‘now’為當前日期時間
**轉字串、時間
SELECT date('now'); -->結果:2018-05-05
SELECT time('now'); -->結果:06:55:38
SELECT datetime('now'); -->結果:2018-05-05 06:55:38
SELECT strftime('%Y-%m-%d %H:%M:%S', 'now'); -->結果:2018-05-05 06:55:38
**轉時間戳
select strftime('%s','now'); -->結果:1525502284
select strftime('%s','2018-05-05'); -->結果:1525478400
時間戳轉時間、字串
select datetime(1525502284, 'unixepoch', 'localtime'); -->結果:2018-05-05 14:38:04
--對引數不理解可參考文末“修飾符”
擴充套件:SQLite的五個時間函式:
- date(timestring, modifier, modifier, …)**:以 YYYY-MM-DD 格式返回日期
- time(timestring, modifier, modifier, …)**:以 HH:MM:SS 格式返回時間
- datetime(timestring, modifier, modifier, …)**:以 YYYY-MM-DD HH:MM:SS 格式返回日期時間
- julianday(format, timestring, modifier, modifier, ..):返回從格林尼治時間的公元前 4714 年 11 月 24 日正午算起的天數
- strftime(format, timestring, modifier, modifier, ..):根據第一個引數指定的格式字串返回格式化的日期
講道理其他四個函式都可以用 strftime() 函式來表示:
date(…) –> strftime(‘%Y-%m-%d’,…)
time(…) –> strftime(‘%H:%M:%S’,…)
datetime(…) –> strftime(‘%Y-%m-%d %H:%M:%S’,…)
julianday(…) –> strftime(‘%J’,…)
來看看各引數的取值格式(文末舉例):
日期時間字串(timestring):
序號 | 日期時間字串 | 例項 |
---|---|---|
1 | YYYY-MM-DD | 2018-05-05 |
2 | YYYY-MM-DD HH:MM | 2018-05-05 12:10 |
3 | YYYY-MM-DD HH:MM:SS.SSS | 2018-05-05 15:39:20.100 |
4 | MM-DD-YYYY HH:MM | 05-05-2018 12:10 |
5 | HH:MM | 同理 |
6 | YYYY-MM-DDTHH:MM | 同理 |
7 | HH:MM:SS | 同理 |
8 | YYYYMMDD HHMMSS | 同理 |
9 | now | 2018-05-05 15:39:20 |
10 | DDDDDDDDDD | 1525478400(時間戳) |
strftime() 函式,格式化串(format):
符號 | 描述 |
---|---|
%d | 一月中的第幾天 01-31 |
%f | 小數形式的秒,SS.SSSS |
%H | 小時 00-24 |
%j | 一年中的第幾天 01-366 |
%J | Julian Day Numbers |
%m | 月份 01-12 |
%M | 分鐘 00-59 |
%s | 從 1970-01-01日開始計算的秒數 |
%S | 秒 00-59 |
%w | 星期,0-6,0是星期天 |
%W | 一年中的第幾周 00-53 |
%Y | 年份 0000-9999 |
%% | % 百分號 |
修飾符(modifier):
序號 | 符號 | 作用 |
---|---|---|
1 | [+-]NNN years | 增加/減去指定數值的年 |
2 | [+-]NNN months | 增加/減去指定數值的月 |
3 | [+-]NNN days | 增加/減去指定數值的天 |
4 | [+-]NNN hours | 增加/減去指定數值的小時 |
5 | [+-]NNN minutes | 增加/減去指定數值的分鐘 |
6 | [+-]NNN.NNNN seconds | 增加/減去指定數值的秒 |
7 | start of year | 當前日期的開始年 |
8 | start of month | 當前日期的開始月 |
9 | start of day | 當前日期的開始日 |
11 | weekday N | 表示返回下一個星期是N的日期和時間 |
12 | unixepoch | 用於將日期解釋為UNIX時間(即:自1970-01-01以來的秒數,也就是時間戳) |
13 | localtime | 表示返回本地時間 |
14 | utc | 表示返回UTC(世界統一時間)時間 |
重點:修飾符運用例項
SELECT datetime('now'); -->結果:2018-05-05 08:10:26(和本機時間可能不一致,時區問題請看使用‘localtime’修飾符後的變化)
SELECT datetime('now','1 years');-->結果:2019-05-05 08:10:26
SELECT datetime('now','1 months');-->結果:2018-06-05 08:10:26
SELECT datetime('now','1 days');-->結果:2018-05-06 08:10:26
SELECT datetime('now','1 hours');-->結果:2018-05-05 09:10:26
SELECT datetime('now','1 minutes');-->結果:2018-05-05 08:11:26
SELECT datetime('now','1 seconds');-->結果:2018-05-05 08:10:27
SELECT datetime('now','start of year');-->結果:2018-01-01 00:00:00
SELECT datetime('now','start of month');-->結果:2018-05-01 00:00:00
SELECT datetime('now','start of day');-->結果:2018-05-05 00:00:00
SELECT datetime('now','weekday 0');-->結果:2018-05-06 08:10:26
(解釋:2018-05-05是週六,“weekday 0”表示返回下週的週日(系統預設以週日為一週的開始0),明天06號是週日,所以,返回了2018-05-06)
SELECT datetime('1525478400','unixepoch');-->結果:2018-05-05 00:00:00(unixepoch一般用於解釋時間戳)
SELECT datetime('now','localtime');-->結果:2018-05-05 16:10:26(你會發現這個時間比沒使用‘localtime’引數的時間多了8個小時,因為中國是東八時區)
SELECT datetime('now','utc');-->結果:2018-05-05 00:10:26(時區問題)
水平有限,小文如有錯漏,請各路大神多指點。