mysql函數(shù)日期和時間函數(shù)匯總
前言
日期和時間函數(shù)主要用來處理日期和時間值,一般的日期函數(shù)除了使用
date
類型的參數(shù)外,也可以使用datetime
或者timestamp
類型的參數(shù),但會忽略這些值的時間部分。
獲取當前日期的函數(shù)
curdate()
和current_date()
函數(shù)的作用相同,將當前日期按照yyyy-mm-dd
或yyyymmdd
格式的值返回。
mysql> select curdate(), current_date(), curdate() + 0; +------------+----------------+---------------+ | curdate() | current_date() | curdate() + 0 | +------------+----------------+---------------+ | 2022-07-12 | 2022-07-12 | 20220712 | +------------+----------------+---------------+ 1 row in set (0.00 sec) mysql>
從上面看到,兩個函數(shù)的作用是相同的,都返回了相同的系統(tǒng)當前日期。
curdate()+0
將當前日期轉換為數(shù)值型。
獲取當前時間的函數(shù)
curtime()
和current_time()
函數(shù)的作用相同,將當前時間以hh:mm:ss
或者hhmmss
的格式返回。
mysql> select curtime(), current_time(), curtime()+0; +-----------+----------------+-------------+ | curtime() | current_time() | curtime()+0 | +-----------+----------------+-------------+ | 21:32:25 | 21:32:25 | 213225 | +-----------+----------------+-------------+ 1 row in set (0.00 sec) mysql>
從上面看到,兩個函數(shù)的作用是相同的,都反悔了相同的系統(tǒng)當前時間。
獲取當前日期和時間的函數(shù)
current_timestamp()
、localtime()
、now()
和sysdate()
這4個函數(shù)的作用相同,均為返回當前日期和時間的值。
mysql> select current_timestamp(), localtime(), now(), sysdate(); +---------------------+---------------------+---------------------+---------------------+ | current_timestamp() | localtime() | now() | sysdate() | +---------------------+---------------------+---------------------+---------------------+ | 2022-07-12 21:34:52 | 2022-07-12 21:34:52 | 2022-07-12 21:34:52 | 2022-07-12 21:34:52 | +---------------------+---------------------+---------------------+---------------------+ 1 row in set (0.00 sec) mysql>
從上面看到,4個函數(shù)返回的結果是一樣的。
UNIX時間戳函數(shù)
UNIX_TIMESTAMP(date)
若無參數(shù)調(diào)用,則返回一個UNIX
時間戳(‘1970-01-01 00:00:00’GMT之后的秒數(shù))作為無符號整數(shù)。
其中,GMT(Green wich mean time)
為格林尼治標準時間。若用date
來調(diào)用UNIX_TIMESTAMP()
,它會將參數(shù)值以1970-01-0100:00:00GMT
后的秒數(shù)的形式返回。
date
可以是一個DATE
字符串、DATETIME
字符串、TIMESTAMP
或一個當?shù)貢r間的YYMMDD
或YYYYMMDD
格式的數(shù)字。
mysql> select unix_timestamp(), unix_timestamp(now()), now(); +------------------+-----------------------+---------------------+ | unix_timestamp() | unix_timestamp(now()) | now() | +------------------+-----------------------+---------------------+ | 1657633074 | 1657633074 | 2022-07-12 21:37:54 | +------------------+-----------------------+---------------------+ 1 row in set (0.00 sec) mysql>
from_unixtime(date)
函數(shù)把unix
時間戳轉換為普通格式時間,與unix_timestamp(date)
函數(shù)互為反函數(shù)。示例:
mysql> select from_unixtime('1657633074'); +-----------------------------+ | from_unixtime('1657633074') | +-----------------------------+ | 2022-07-12 21:37:54.000000 | +-----------------------------+ 1 row in set (0.00 sec) mysql>
返回UTC日期的函數(shù)
utc_date()
函數(shù)返回當前utc
(世界標準時間)日期值,其格式為yyyy-mm-dd
或者yyyymmdd
;
mysql> select utc_date(), utc_date()+0; +------------+--------------+ | utc_date() | utc_date()+0 | +------------+--------------+ | 2022-07-12 | 20220712 | +------------+--------------+ 1 row in set (0.00 sec) mysql>
返回UTC時間的函數(shù)
utc_time()
返回當前utc
時間值,格式為hh:mm:ss
或者hhmmss
;
mysql> select utc_time(), utc_time()+0; +------------+--------------+ | utc_time() | utc_time()+0 | +------------+--------------+ | 13:43:24 | 134324 | +------------+--------------+ 1 row in set (0.00 sec) mysql>
獲取月份的函數(shù)MONTH(date)和MONTHNAME(date)
month(date)
函數(shù)返回date
對應的月份。
mysql> select month('2022-12-12') as coll, month('20221212') as coll_1, month('221212') as coll_2; +------+--------+--------+ | coll | coll_1 | coll_2 | +------+--------+--------+ | 12 | 12 | 12 | +------+--------+--------+ 1 row in set (0.00 sec) mysql>
monthname(date)
函數(shù)返回日期date
對應月份的英文全名;
mysql> select monthname('2022-12-12'), monthname('20221212'), monthname('221212'); +-------------------------+-----------------------+---------------------+ | monthname('2022-12-12') | monthname('20221212') | monthname('221212') | +-------------------------+-----------------------+---------------------+ | December | December | December | +-------------------------+-----------------------+---------------------+ 1 row in set (0.00 sec) mysql>
獲取星期的函數(shù)DAYNAME(d)、DAYOFWEEK(d)和WEEKDAY(d)
dayname(d)
函數(shù)返回d
對應的工作日的英文名稱;
mysql> select dayname('2022-07-12'); +-----------------------+ | dayname('2022-07-12') | +-----------------------+ | Tuesday | +-----------------------+ 1 row in set (0.00 sec) mysql>
dayofweek(d)
函數(shù)返回d
對應的一周中的索引(位置,1表示周日,2表示周一,以此類推,7表示周六);
mysql> select dayofweek('2022-07-12') as coll, dayofweek('2022-07-13') as coll_1; +------+--------+ | coll | coll_1 | +------+--------+ | 3 | 4 | +------+--------+ 1 row in set (0.00 sec) mysql>
weekday(d)
返回d
對應的工作日索引:0表示周一,1表示周二,以此類推,6表示周日。
mysql> select weekday('2022-07-12') as coll, weekday('2022-07-13') as coll_1; +------+--------+ | coll | coll_1 | +------+--------+ | 1 | 2 | +------+--------+ 1 row in set (0.00 sec) mysql>
獲取星期的函數(shù)WEEK(d)和WEEKOFYEAR(d)
week(d)
計算日期d
是一年中的第幾周。
week()
的雙參數(shù)形式允許指定該星期是否始于周日或者周一,以及返回值的范圍是否為0~53或者1~53.若mode
參數(shù)被省略,則使用default_week_format
系統(tǒng)自變量的值,如下圖:
例如:使用week()
函數(shù)查詢指定日期是一年中的第幾周;
mysql> select week('2022-07-13'), week('2022-01-01'), week('2022-09-18'); +--------------------+--------------------+--------------------+ | week('2022-07-13') | week('2022-01-01') | week('2022-09-18') | +--------------------+--------------------+--------------------+ | 28 | 0 | 38 | +--------------------+--------------------+--------------------+ 1 row in set (0.00 sec) mysql>
weekofyear(d)
計算某一天位于一年中的第幾周,范圍是1~53,相當于week(d,3)
;
例如:使用weekofyear(d)
查詢指定日期是一年中的第幾周;
mysql> select weekofyear('2022-07-13'), week('2022-07-13', 3); +--------------------------+-----------------------+ | weekofyear('2022-07-13') | week('2022-07-13', 3) | +--------------------------+-----------------------+ | 28 | 28 | +--------------------------+-----------------------+ 1 row in set (0.00 sec) mysql>
從上面可以看出,兩個函數(shù)返回結果相同。
獲取天數(shù)的函數(shù)DAYOFYEAR(d)和DAYOFMONTH(d)
dayofyear(d)
函數(shù)返回d
是一年中的第幾天,范圍是1~366;
例如:使用dayofyear()
函數(shù)返回指定日期在一年中的位置;
mysql> select dayofyear('2022-07-13'), dayofyear('2022-01-01'); +-------------------------+-------------------------+ | dayofyear('2022-07-13') | dayofyear('2022-01-01') | +-------------------------+-------------------------+ | 194 | 1 | +-------------------------+-------------------------+ 1 row in set (0.00 sec) mysql>
dayofmonth(d)
函數(shù)返回d
是一個月中的第幾天,范圍是1~31;
例如:使用dayofmonth()
函數(shù)返回指定日期在一個月中的位置,必須有具體年份才可以。
mysql> select dayofmonth('2022-07-13'), dayofmonth('220713'), dayofmonth('0713'); +--------------------------+----------------------+--------------------+ | dayofmonth('2022-07-13') | dayofmonth('220713') | dayofmonth('0713') | +--------------------------+----------------------+--------------------+ | 13 | 13 | NULL | +--------------------------+----------------------+--------------------+ 1 row in set, 1 warning (0.00 sec) mysql>
獲取年份、季度、小時、分鐘和秒鐘的函數(shù)
year(date)
返回date
對應的年份,范圍是1970~2069
;
mysql> select year('2022-07-13'), year('20330909'); +--------------------+------------------+ | year('2022-07-13') | year('20330909') | +--------------------+------------------+ | 2022 | 2033 | +--------------------+------------------+ 1 row in set (0.00 sec) mysql>
小提示:
00~69
轉換為2000~2069
,70~99
轉換為1970~1999
。
quarter(date)
返回date
對應的一年中的季度值,范圍是1~4;
mysql> select quarter('2022-07-13'), quarter('20330101'); +-----------------------+---------------------+ | quarter('2022-07-13') | quarter('20330101') | +-----------------------+---------------------+ | 3 | 1 | +-----------------------+---------------------+ 1 row in set (0.00 sec) mysql>
minute(time)
返回time
對應的分鐘數(shù),范圍是0~59。
mysql> select minute('2022-07-13 09:09:09'), minute('06:06:06'); +-------------------------------+--------------------+ | minute('2022-07-13 09:09:09') | minute('06:06:06') | +-------------------------------+--------------------+ | 9 | 6 | +-------------------------------+--------------------+ 1 row in set (0.00 sec) mysql>
second(time)
返回time
對應的秒數(shù),范圍是0~59。
mysql> select second('2022-07-13 09:09:09'), second('06:06:06'); +-------------------------------+--------------------+ | second('2022-07-13 09:09:09') | second('06:06:06') | +-------------------------------+--------------------+ | 9 | 6 | +-------------------------------+--------------------+ 1 row in set (0.00 sec) mysql>
獲取日期的指定值的函數(shù)EXTRACT(type from date)
extract(type from date)
函數(shù)所使用的時間間隔類型說明符與date_add()
或date_sub()
相同,但它從日期中提取一部分,而不是執(zhí)行日期運算。
mysql> select extract(year from '2022-07-13') as coll, extract(year_month from '2022-07-13') as coll_1, extract(day_minute from '2022-07-13 09:08:07') as coll_2; +------+--------+--------+ | coll | coll_1 | coll_2 | +------+--------+--------+ | 2022 | 202207 | 130908 | +------+--------+--------+ 1 row in set (0.00 sec) mysql>
type
為不同值時,返回的值不同:
year
:只返回年值year_month
:返回年與月份day_minute
:返回日、小時和分鐘值
時間和秒鐘轉換的函數(shù)
time_to_sec(time)
返回已轉化為秒的time
參數(shù)。
轉換公式為:小時*3600+分鐘*60+秒
mysql> select time_to_sec('09:09:09'); +-------------------------+ | time_to_sec('09:09:09') | +-------------------------+ | 32949 | +-------------------------+ 1 row in set (0.00 sec) mysql>
sec_to_time(seconds)
返回被轉化為小時、分鐘和秒數(shù)的seconds
參數(shù)值,其格式為hh:mm:ss
或者hhmmss
。
mysql> select time_to_sec('09:09:09'), sec_to_time(32949); +-------------------------+--------------------+ | time_to_sec('09:09:09') | sec_to_time(32949) | +-------------------------+--------------------+ | 32949 | 09:09:09 | +-------------------------+--------------------+ 1 row in set (0.00 sec) mysql>
從上面可以看到,time_to_sec
和sec_to_time
互為反函數(shù)。
計算日期和時間的函數(shù)
計算日期和時間的函數(shù)有:
- date_add()
- adddate()
- date_sub()
- subdate()
- addtime()
- subtime()
- date_diff()
在date_add(date, interval expr type)
和date_sub(date, interval expr type)
中:
date
是一個datetime
或者date
值,用來指定起始時間。expr
是一個表達式,用來指定從起始日期添加或減去的時間間隔值。對于負值的時間間隔,expr
可以以一個符號-
開頭。type
為關鍵詞,指示了表達式被解釋的方式。
若date
參數(shù)是一個date
值,計算只會包含year
、month
和day
部分(沒有時間部分),其結果是一個date
值,否則,結果將是一個datetime
值。
date_add(date, interval expr type)
和adddate(date, intervar expr type)
兩個函數(shù)的作用相同,執(zhí)行日期的加運算。
mysql> select date_add('2022-07-13 09:09:09', interval 1 second) as coll, adddate('2022-07-13 09:09:09', interval 1 second) as coll_1, date_add('2022-07-13 09:09:09', interval '1:1' minute_second) as coll_2; +---------------------+---------------------+---------------------+ | coll | coll_1 | coll_2 | +---------------------+---------------------+---------------------+ | 2022-07-13 09:09:10 | 2022-07-13 09:09:10 | 2022-07-13 09:10:10 | +---------------------+---------------------+---------------------+ 1 row in set (0.00 sec) mysql>
date_sub(date, interval expr type)
或者subdate(date, interval expr type)
兩個函數(shù)的作用相同,執(zhí)行日期的減運算。
mysql> select date_sub('2022-07-13 09:09:09', interval 31 day) as coll, subdate('2022-07-13 09:09:09', interval 31 day) as coll_1, date_sub('2022-07-13 09:09:09', interval '0 0:1:1' day_second) as coll_2; +---------------------+---------------------+---------------------+ | coll | coll_1 | coll_2 | +---------------------+---------------------+---------------------+ | 2022-06-12 09:09:09 | 2022-06-12 09:09:09 | 2022-07-13 09:08:08 | +---------------------+---------------------+---------------------+ 1 row in set (0.00 sec) mysql>
addtime(date, expr)
函數(shù)將expr
值添加到date
,并返回修改后的值,date
值一個日期或者日期時間表達式,而expr
是一個時間表達式。
mysql> select adddate('2022-07-13 09:09:09', '1:1:1'), addtime('09:09:09', '1:1:20'); +-----------------------------------------+-------------------------------+ | adddate('2022-07-13 09:09:09', '1:1:1') | addtime('09:09:09', '1:1:20') | +-----------------------------------------+-------------------------------+ | 2022-07-14 09:09:09 | 10:10:29 | +-----------------------------------------+-------------------------------+ 1 row in set, 1 warning (0.00 sec) mysql>
subtime(date,expr)
函數(shù)將date
減去expr
值,并返回修改后的值。
其中,date
是一個日期或者日期時間表達式,而expr
是一個時間表達式。
mysql> select subtime('2020-07-13 09:09:09', '1:1:1 1:1:1'), subtime('2022-07-13 09:09:09', '1:1:1'); +-----------------------------------------------+-----------------------------------------+ | subtime('2020-07-13 09:09:09', '1:1:1 1:1:1') | subtime('2022-07-13 09:09:09', '1:1:1') | +-----------------------------------------------+-----------------------------------------+ | 2020-07-13 08:08:08 | 2022-07-13 08:08:08 | +-----------------------------------------------+-----------------------------------------+ 1 row in set, 2 warnings (0.00 sec) mysql>
從上面可以看到,只計算了時間,日期并沒有計算進去。
使用datediff()
函數(shù)計算兩個日期之間的間隔天數(shù);
mysql> select datediff('2022-07-13 09:09:09', '2022-01-01') as coll, datediff('2022-07-13 09:09:09', '2022-07-10 08:08:08'); +------+--------------------------------------------------------+ | coll | datediff('2022-07-13 09:09:09', '2022-07-10 08:08:08') | +------+--------------------------------------------------------+ | 193 | 3 | +------+--------------------------------------------------------+ 1 row in set (0.00 sec) mysql>
將日期和時間格式化的函數(shù)
date_format(date,format)
根據(jù)format
指定的格式顯示date
值。
主要format
格式如下圖:
示例:使用date_format()
函數(shù)格式化輸出日期和時間值;
mysql> select date_format('2022-07-13 09:08:07', '%W %M %Y') as coll, date_format('2022-07-13 09:08:07', '%D %y %a %d %m %b %j') as coll_1; +---------------------+---------------------------+ | coll | coll_1 | +---------------------+---------------------------+ | Wednesday July 2022 | 13th 22 Wed 13 07 Jul 194 | +---------------------+---------------------------+ 1 row in set (0.00 sec) mysql>
time_format(time, format)
根據(jù)表達式format
的要求顯示時間time
。
表達式format
指定了顯示的格式。因為time_format(time, format)
只處理時間,所以format
只使用時間格式。
示例:使用time_format()
函數(shù)格式化輸入時間值。
mysql> select time_format('13:14:15', '%H %k %h %I %l'); +-------------------------------------------+ | time_format('13:14:15', '%H %k %h %I %l') | +-------------------------------------------+ | 13 13 01 01 1 | +-------------------------------------------+ 1 row in set (0.00 sec) mysql>
get_format(val_typr, format_type)
返回日期時間字符串的顯示格式:
val_type
表示日期數(shù)據(jù)類型,包括date
、datetime
和time;
format_type
表示格式化顯示類型,包括eur
、interval
、iso
、jis
、usa
。
get_format
根據(jù)兩個值類型組合返回的字符串顯示格式如下:
示例:使用get_format()
函數(shù)顯示不同格式化類型下的格式字符串。
mysql> select get_format(date, 'eur'), get_format(datetime, 'jis'); +-------------------------+-----------------------------+ | get_format(date, 'eur') | get_format(datetime, 'jis') | +-------------------------+-----------------------------+ | %d.%m.%Y | %Y-%m-%d %H:%i:%s | +-------------------------+-----------------------------+ 1 row in set (0.00 sec) mysql> mysql> select date_format('2022-07-13 09:08:07', get_format(datetime, 'jis')) as coll, date_format('2022-07-13 09:08:07', get_format(date, 'usa')) as coll_1; +---------------------+------------+ | coll | coll_1 | +---------------------+------------+ | 2022-07-13 09:08:07 | 07.13.2022 | +---------------------+------------+ 1 row in set (0.00 sec) mysql>
到此這篇關于mysql函數(shù)日期和時間函數(shù)匯總的文章就介紹到這了,更多相關mysql日期函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
如何在SQL Server中實現(xiàn) Limit m,n 的功能
本篇文章是對在SQL Server中實現(xiàn) Limit m,n功能的方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06MySQL數(shù)據(jù)庫導入導出數(shù)據(jù)之報錯解答實例講解
這篇文章主要介紹了MySQL數(shù)據(jù)庫導入導出數(shù)據(jù)之報錯解答實例講解,文中對報錯和解決方法做了詳細的實例展示,有需要的同學可以借鑒參考下2021-02-02