共計(jì) 12392 個(gè)字符,預(yù)計(jì)需要花費(fèi) 31 分鐘才能閱讀完成。
自動(dòng)寫(xiě)代碼機(jī)器人,免費(fèi)開(kāi)通
丸趣 TV 小編給大家分享一下 MySQL 中日期函數(shù)有哪些,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
日期函數(shù)類型:
(1)獲取當(dāng)前日期的函數(shù)和獲取當(dāng)前時(shí)間的函數(shù)
(2) 獲取當(dāng)前日期和時(shí)間的函數(shù)
(3)UNIX 時(shí)間戳函數(shù)
(4) 返回 UTC 日期的函數(shù)和返回 UTC 時(shí)間的函數(shù)
(5) 獲取月份的函數(shù) month(date)和 monthname(date)
(6)獲取星期的函數(shù) dayname(d)、dayofweek(d)和 weekday(d)
(7)獲取星期數(shù)的函數(shù) week(d)和 dayofyear(d)
(8)獲取天數(shù)的函數(shù) dayofyear(d)和 dayofmonth(d)
(9)獲取年份、季度、小時(shí)、分鐘和秒鐘的函數(shù)。
(10)獲取日期的指定值的函數(shù) extract(type from date)
(11)時(shí)間和秒鐘轉(zhuǎn)換的函數(shù)
(12) 計(jì)算日期和時(shí)間的函數(shù)
(13) 將日期和時(shí)間格式化的函數(shù)
(相關(guān)免費(fèi)學(xué)習(xí)推薦:mysql 視頻教程)
(1)獲取當(dāng)前日期的函數(shù)和獲取當(dāng)前時(shí)間的函數(shù)
1.curdate()和 current_date()
【例】使用日期函數(shù)獲取系統(tǒng)當(dāng)前日期,SQL 語(yǔ)句如下:
mysql select curdate(),current_date(),curdate()+0;+------------+----------------+-------------+| curdate() | current_date() | curdate()+0 |+------------+----------------+-------------+| 2019-08-18 | 2019-08-18 | 20190818 |+------------+----------------+-------------+1 row in set (0.00 sec)
curdate()+ 0 表示將當(dāng)前日期值轉(zhuǎn)換為數(shù)值型。
2.curtime()和 current_time()
【例】使用時(shí)間函數(shù)獲取系統(tǒng)當(dāng)前時(shí)間,SQL 語(yǔ)句如下:
mysql select curtime(),current_time(),curtime()+0;+-----------+----------------+-------------+| curtime() | current_time() | curtime()+0 |+-----------+----------------+-------------+| 17:08:07 | 17:08:07 | 170807 |+-----------+----------------+-------------+1 row in set (0.00 sec)
curtime()+ 0 表示將當(dāng)前時(shí)間值轉(zhuǎn)換為數(shù)值型。
(2)獲取當(dāng)前日期和時(shí)間的函數(shù)
current_timestamp()、localtime()、now()和 sysdate()四個(gè)函數(shù)作用相同,都是返回當(dāng)前日期和時(shí)間值。
【例】使用日期時(shí)間函數(shù)獲取當(dāng)前系統(tǒng)時(shí)間和日期,SQL 語(yǔ)句如下:
mysql select current_timestamp(),
- localtime(),
- now(),
- sysdate();+---------------------+---------------------+---------------------+---------------------+| current_timestamp() | localtime() | now() | sysdate() |+---------------------+---------------------+---------------------+---------------------+| 2019-08-18 19:10:05 | 2019-08-18 19:10:05 | 2019-08-18 19:10:05 | 2019-08-18 19:10:05 |+---------------------+---------------------+---------------------+---------------------+1 row in set (0.05 sec)
(3)UNIX 時(shí)間戳函數(shù)
unix_timestamp(date)若為無(wú)參數(shù)調(diào)用, 則返回一個(gè) unix 時(shí)間戳 (GMT 之后的秒數(shù),GMT 為格林尼治標(biāo)準(zhǔn)時(shí)間 1970.1.1) 作為無(wú)符號(hào)整數(shù)。
date 可以是一個(gè) date 字符串,datetime 字符串、timestamp 或一個(gè)當(dāng)?shù)貢r(shí)間的 YY[YY]MMDD 格式的數(shù)字。
1.unix_timestamp(date)
【例】使用 unix_timestamp 函數(shù)返回 unix 格式的時(shí)間戳,SQL 語(yǔ)句如下:
mysql select unix_timestamp(),unix_timestamp(now()),now();+------------------+-----------------------+---------------------+| unix_timestamp() | unix_timestamp(now()) | now() |+------------------+-----------------------+---------------------+| 1566127316 | 1566127316 | 2019-08-18 19:21:56 |+------------------+-----------------------+---------------------+1 row in set (0.05 sec)
from_unixtime()函數(shù)吧 unix 時(shí)間戳轉(zhuǎn)換為普通格式的時(shí)間, 與 unix_timestamp(date)函數(shù)互為反函數(shù)。
2.from_unixtime(date)
【例】使用 from_unixtime 函數(shù)將 unix 時(shí)間戳轉(zhuǎn)換為普通格式時(shí)間,SQL 語(yǔ)句如下:
mysql select from_unixtime(1566127316 +-----------------------------+| from_unixtime( 1566127316) |+-----------------------------+| 2019-08-18 19:21:56.000000 |+-----------------------------+1 row in set (0.00 sec)
(4)返回 UTC 日期的函數(shù)和返回 UTC 時(shí)間的函數(shù)。
1.UTC_DATE()
【例】使用 utc_date()函數(shù)返回當(dāng)前 UTC 日期值,SQL 語(yǔ)句如下:
mysql select utc_date(),utc_date()+0;+------------+--------------+| utc_date() | utc_date()+0 |+------------+--------------+| 2019-08-18 | 20190818 |+------------+--------------+1 row in set (0.05 sec)
2.UTC_TIME()
【例】使用 UTC_TIME()函數(shù)返回當(dāng)前 UTC 時(shí)間值,SQL 語(yǔ)句如下:
mysql select utc_time(),utc_time()+0;+------------+--------------+| utc_time() | utc_time()+0 |+------------+--------------+| 11:32:27 | 113227 |+------------+--------------+1 row in set (0.00 sec)
(5)獲取月份的函數(shù) month(date)和 monthname(date)
1.month(date)
【例】使用 month()函數(shù)返回指定日期中的月份,SQL 語(yǔ)句如下:
mysql select month(2019-08-18 +---------------------+| month( 2019-08-18) |+---------------------+| 8 |+---------------------+1 row in set (0.00 sec)
2.monthname(date)
【例】使用 monthname()函數(shù)返回指定日期中的月份名稱,SQL 語(yǔ)句如下:
mysql select monthname(2019-08-18 +-------------------------+| monthname( 2019-08-18) |+-------------------------+| August |+-------------------------+1 row in set (0.00 sec)
(6)獲取星期的函數(shù) dayname(d)、dayofweek(d)和 weekday(d)
1.dayname(d)
【例】使用 dayname()函數(shù)返回指定日期的工作日名稱,SQL 語(yǔ)句如下:
mysql select dayname(2019-08-18 +-----------------------+| dayname( 2019-08-18) |+-----------------------+| Sunday |+-----------------------+1 row in set (0.00 sec)
2.dayofweek(d)
【例】使用 dayofweek()函數(shù)返回日期對(duì)應(yīng)的周索引,SQL 語(yǔ)句如下:
mysql select dayofweek(2019-08-18 +-------------------------+| dayofweek( 2019-08-18) |+-------------------------+| 1 |+-------------------------+1 row in set (0.00 sec)
3.weekday(d)
weekday(d)返回 d 對(duì)應(yīng)的工作日索引:0 代表周一,1 代代表周二,…6 代表周日。
【例】使用 weekday()函數(shù)返回日期對(duì)應(yīng)的工作日索引,SQL 語(yǔ)句如下:
mysql select weekday(2019-08-18 19:40:00),
- weekday(2019-08-18 +--------------------------------+-----------------------+| weekday( 2019-08-18 19:40:00) | weekday(2019-08-18) |+--------------------------------+-----------------------+| 6 | 6 |+--------------------------------+-----------------------+1 row in set (0.00 sec)
(7)獲取星期數(shù)的函數(shù) week(d)和 dayofyear(d)
week(d)計(jì)算日期 d 是一年中第幾周,雙參形式允許指定該星期是否起始于周日或周一,若 Mode 參數(shù)被忽略,使用 default_week_format 系統(tǒng)自變量的值。
1.week(d)
【例】使用 week()函數(shù)查詢指定日期是一年中的第幾周,SQL 語(yǔ)句如下:
mysql select week(2019-08-18),week(2019-08-18 ,0),week(2019-08-18 ,1);+--------------------+----------------------+----------------------+| week(2019-08-18) | week(2019-08-18 ,0) | week(2019-08-18 ,1) |+--------------------+----------------------+----------------------+| 33 | 33 | 33 |+--------------------+----------------------+----------------------+1 row in set (0.05 sec
2.weekofyear(d)
【例】使用 weekofyear()查詢指定日期是一年中的第幾周,SQL 語(yǔ)句如下:
mysql select week(2019-08-18 ,3),weekofyear(2019-08-18 +----------------------+--------------------------+| week( 2019-08-18 ,3) | weekofyear(2019-08-18) |+----------------------+--------------------------+| 33 | 33 |+----------------------+--------------------------+1 row in set (0.05 sec)
(8)獲取天數(shù)的函數(shù) dayofyear(d)和 dayofmonth(d)
1.dayofyear()
【例】使用 dayofyear()函數(shù)返回指定日期在一年中的位置,SQL 語(yǔ)句如下:
mysql select dayofyear(2019-08-18 +-------------------------+| dayofyear( 2019-08-18) |+-------------------------+| 230 |+-------------------------+1 row in set (0.00 sec)
2.dayofmonth()
【例】使用 dayofmonth()函數(shù)返回指定日期在一個(gè)月中的位置,SQL 語(yǔ)句如下;
mysql select dayofmonth(2019-08-18 +--------------------------+| dayofmonth( 2019-08-18) |+--------------------------+| 18 |+--------------------------+1 row in set (0.00 sec)
(9)獲取年份、季度、小時(shí)、分鐘和秒鐘的函數(shù)。
1.YEAR(date)
【例】使用 year()函數(shù)返回指定日期對(duì)應(yīng)的年份,SQL 語(yǔ)句如下:
mysql select year(19-08-18),year(98-02-19 +------------------+------------------+| year( 19-08-18) | year(98-02-19) |+------------------+------------------+| 2019 | 1998 |+------------------+------------------+1 row in set (0.05 sec)
2.QUARTER(date)
【例】使用 quarter()函數(shù)返回指定日期對(duì)應(yīng)的季度,SQL 語(yǔ)句如下:
mysql select quarter(19-08-18 +---------------------+| quarter( 19-08-18) |+---------------------+| 3 |+---------------------+1 row in set (0.00 sec)
3.MINUTE(time)
【例】使用 minute()函數(shù)返回指定時(shí)間的分鐘值,SQL 語(yǔ)句如下:
mysql select minute(19-08-18 20:07:00 +-----------------------------+| minute( 19-08-18 20:07:00) |+-----------------------------+| 7 |+-----------------------------+1 row in set (0.00 sec)
4.SECOND(time)
【例】使用 second()函數(shù)返回指定時(shí)間的秒值,SQL 語(yǔ)句如下:
mysql select second(20:07:00 +--------------------+| second( 20:07:00) |+--------------------+| 0 |+--------------------+1 row in set (0.00 sec)
(10)獲取日期的指定值的函數(shù) extract(type from date)
【例】使用 extract(type from date)函數(shù)提取日期或時(shí)間值。
mysql select extract(year from 2019-08-18) as col1,
- extract(year_month from 2019-08-18 20:46:01) as col2,
- extract(day_minute from 2019-08-18 20:46:01) as col3;+------+--------+--------+| col1 | col2 | col3 |+------+--------+--------+| 2019 | 201908 | 182046 |+------+--------+--------+1 row in set (0.00 sec)
(11)時(shí)間和秒鐘轉(zhuǎn)換的函數(shù)
1.time_to_sec(time)
time_to_sec(time)返回已經(jīng)轉(zhuǎn)化為秒的 time 參數(shù)。轉(zhuǎn)換公式為:小時(shí) x3600+ 分鐘 *60+ 秒。
【例】使用 time_to_sec 函數(shù)將時(shí)間值轉(zhuǎn)換為秒值。
mysql select time_to_sec(20:34:00 +-------------------------+| time_to_sec( 20:34:00) |+-------------------------+| 74040 |+-------------------------+1 row in set (0.00 sec)
2.sec_to_time(seconds)
sec_to_time 函數(shù)返回值加上 0 值之后變成了小數(shù)值。
time_to_sec 正好和 sec_to_time 互為反函數(shù)。
【例】使用 sec_to_time()函數(shù)將秒值轉(zhuǎn)換為時(shí)間格式,SQL 語(yǔ)句如下;
mysql select sec_to_time(2345),sec_to_time(2345)+0,
- time_to_sec(20:36:00),sec_to_time(74040 +-------------------+---------------------+-------------------------+----------------------+| sec_to_time(2345) | sec_to_time(2345)+0 | time_to_sec(20:36:00) | sec_to_time(74040) |+-------------------+---------------------+-------------------------+----------------------+| 00:39:05 | 3905 | 74160 | 20:34:00.000000 |+-------------------+---------------------+-------------------------+----------------------+1 row in set (0.05 sec)
(12)計(jì)算日期和時(shí)間的函數(shù)。
MySQL 中計(jì)算日期和時(shí)間的格式:
1.date_add(date,interval expr type)和 adddate(date,interval expr type)兩個(gè)函數(shù)的作用相同, 執(zhí)行日期的加運(yùn)算:
【例】使用 date_add()和 adddate()函數(shù)執(zhí)行日期加操作,SQL 語(yǔ)句如下:
mysql select date_add(2019-08-18 23:59:59 ,interval 1 second) as col1,
- adddate(2019-08-18 23:59:59 ,interval 1 second) as col2,
- date_add(2019-08-18 23:59:59 ,interval 1:1 minute_second) as col3;+---------------------+---------------------+---------------------+| col1 | col2 | col3 |+---------------------+---------------------+---------------------+| 2019-08-19 00:00:00 | 2019-08-19 00:00:00 | 2019-08-19 00:01:00 |+---------------------+---------------------+---------------------+1 row in set (0.05 sec)
2.date_sub(date,interval expr type)和 subdate(date,interval expr type)兩個(gè)函數(shù)作用相同,執(zhí)行日期的減運(yùn)算:
【例】使用 date_sub 和 subdate 函數(shù)執(zhí)行日期減操作,SQL 語(yǔ)句如下:
mysql select date_sub(2019-08-18 ,interval 31 day) as col1,
- subdate(2019-08-18 ,interval 31 day) as col2,
- date_sub(2019-08-18 21:15:10 ,interval 0 0:1:1 day_second) as col3;+------------+------------+---------------------+| col1 | col2 | col3 |+------------+------------+---------------------+| 2019-07-18 | 2019-07-18 | 2019-08-18 21:14:09 |+------------+------------+---------------------+1 row in set (0.00 sec)
3.addtime(date,expr)函數(shù)將 expr 值添加到 date, 并返回修改后的值,date 是一個(gè)日期或者日期時(shí)間表達(dá)式, 而 expr 是一個(gè)時(shí)間表達(dá)式。
【例】使用 addtime 進(jìn)行時(shí)間加操作,SQL 語(yǔ)句如下;
mysql select addtime(2019-08-18 21:59:59 , 1:1:1),addtime(02:02:02 , 02:00:00 +----------------------------------------+--------------------------------+| addtime( 2019-08-18 21:59:59 , 1:1:1) | addtime(02:02:02 , 02:00:00) |+----------------------------------------+--------------------------------+| 2019-08-18 23:01:00 | 04:02:02 |+----------------------------------------+--------------------------------+1 row in set (0.05 sec)
4.subtime(date,expr)函數(shù)將 date 減去 expr 值,并返回修改后的值,date 是一個(gè)日期或者日期時(shí)間表達(dá)式,expr 是一個(gè)時(shí)間表達(dá)式。
【例】使用 subtime()函數(shù)執(zhí)行減操作,SQL 語(yǔ)句如下:
mysql select subtime(2019-08-18 21:59:59 , 1:1:1),subtime(02:02:02 , 02:00:00 +----------------------------------------+--------------------------------+| subtime( 2019-08-18 21:59:59 , 1:1:1) | subtime(02:02:02 , 02:00:00) |+----------------------------------------+--------------------------------+| 2019-08-18 20:58:58 | 00:02:02 |+----------------------------------------+--------------------------------+1 row in set (0.00 sec)
5.datediff(date1,date2)返回起始時(shí)間 date1 和結(jié)束時(shí)間 date2 之間的天數(shù),date1 和 date2 為日期或 date-and-time 表達(dá)式。計(jì)算中只用到這些值的日期部分。
【例】使用 datediff()函數(shù)計(jì)算兩個(gè)日期之間的間隔天數(shù),SQL 語(yǔ)句如下;
mysql select datediff(2019-08-18 21:59:59 , 2018-07-18) as col1,
- datediff(2019-08-18 22:00:00 , 2019-08-20) as col2;+------+------+| col1 | col2 |+------+------+| 396 | -2 |+------+------+1 row in set (0.00 sec)
(13)將日期和時(shí)間格式化的函數(shù)。
DATE_FORMAT 時(shí)間日期格式:
1.date_format()
【例】使用 date_format()函數(shù)格式化輸出日期和時(shí)間值,SQL 語(yǔ)句如下:
mysql select date_format(2019-08-18 23:33:00 , %w %m %y) as col1,
- date_format(2019-08-18 23:33:00 , %D %y %a %d %m %b %j) as col2;+---------+---------------------------+| col1 | col2 |+---------+---------------------------+| 0 08 19 | 18th 19 Sun 18 08 Aug 230 |+---------+---------------------------+1 row in set (0.05 sec)
2.time_format()
【例】使用 time_format(time,format)函數(shù)格式化輸入時(shí)間值,SQL 語(yǔ)句如下:
mysql select time_format(23:39:30 , %H %k %h %I %l +------------------------------------------+| time_format( 23:39:30 , %H %k %h %I %l) |+------------------------------------------+| 23 23 11 11 11 |+------------------------------------------+1 row in set (0.00 sec)
3.get_format()
get_format 返回的格式字符串:
【例】使用 get_format()函數(shù)顯示不同格式化類型下的格式字符串,SQL 語(yǔ)句如下:
mysql select get_format(date, eur),get_format(date, usa +------------------------+------------------------+| get_format(date, eur) | get_format(date, usa) |+------------------------+------------------------+| %d.%m.%Y | %m.%d.%Y |+------------------------+------------------------+1 row in set (0.05 sec)
【例】在 date_format()函數(shù)中, 使用 get_format 函數(shù)返回的顯示格式字符串來(lái)顯示指定的日期值,SQL 語(yǔ)句如下:
mysql select date_format(2019-08-19 23:41:30 ,get_format(date, usa +-----------------------------------------------------------+| date_format( 2019-08-19 23:41:30 ,get_format(date, usa)) |+-----------------------------------------------------------+| 08.19.2019 |+-----------------------------------------------------------+1 row in set (0.00 sec)
看完了這篇文章,相信你對(duì)“MySQL 中日期函數(shù)有哪些”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道,感謝各位的閱讀!
向 AI 問(wèn)一下細(xì)節(jié)