MySQL日期/時間函數

一、查詢當前時間函數:函數

select NOW(),LOCALTIME(),SYSDATE(),CURRENT_TIMESTAMP();

可是now()與sysdate()有點差別的,一個語句中now()的值是不變的,而sysdate()是動態獲取的,例如spa

select NOW(),SLEEP(2),NOW();
SELECT SYSDATE(),SLEEP(2),SYSDATE();

-------經過在MySQL中執行select sleep(N)可讓此語句運行N秒鐘3d

二、獲取當前日期,curdate()=current_date()=current_datecode

select curdate(),current_date(),current_date;

三、獲取當前時間,curtime()=current_time()=current_timeorm

select curtime(),current_time(),current_time;

四、獲取年,月,日blog

select year(now()),month(now()),day(now());

五、獲取當前星期幾,幾月,以英文返回,dayname(),monthname()字符串

select dayname(curdate()),monthname(curdate());

六、獲取某個日期在周,月,年中的位置,dayofweek(),dayofmonth,dayofyear(),如要返回中文周幾,能夠在程序中進行邏輯ast

select dayofweek(now()),dayofmonth(now()),dayofyear(now());

七、獲取一個月的最後一天,last_day(),利用它能夠獲得某個月有多少天form

select last_day(NOW()),day(last_day(NOW()));

八、獲取某天位於一年中的第N周week(date,3)=weekofyear(),week()函數的第二個參數用來設定以星期幾作爲一週的開始class

select week(now(),3),weekofyear(now());

九、獲取兩個日期或者兩個時間的相差,datediff(),timediff()

select datediff(curdate(),'2018-02-15'),timediff(curtime(),'09:09:09')

十、時間與秒的轉換,time_to_sec(),sec_to_time()

select time_to_sec(@d),sec_to_time(12389);

十一、日期與天數的轉換,to_days(),from_days()

十二、字符串轉換爲日期,str_to_date(date,format)

select str_to_date('09/09/20','%Y/%m/%d');
select str_to_date('09.09.20','%Y.%m.%d');

1三、日期/時間拼湊,makedate(year,dayofyear),maketime(hour,minute,second)

select makedate(2015,200),maketime(13,20,15);
相關文章
相關標籤/搜索