MySQL日期 時間函式

2022-07-20 19:54:10 字數 1740 閱讀 1507

1、查詢當前時間函式:

select now(),localtime(),sysdate(),current_timestamp();

但是now()與sysdate()有點差異的,乙個語句中now()的值是不變的,而sysdate()是動態獲取的,例如

select now(),sleep(2

),now();

select sysdate(),sleep(2),sysdate();

-------通過在mysql中執行select sleep(n)可以讓此語句執行n秒鐘

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

select curdate(),current_date(),current_date;
3、獲取當前時間,curtime()=current_time()=current_time

select curtime(),current_time(),current_time;
4、獲取年,月,日

select

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

5、獲取當前星期幾,幾月,以英文返回,dayname(),monthname()

select dayname(curdate()),monthname(curdate());
6、獲取某個日期在周,月,年中的位置,dayofweek(),dayofmonth,dayofyear(),如要返回中文週幾,可以在程式中進行邏輯

select dayofweek(now()),dayofmonth(now()),dayofyear(now());
7、獲取乙個月的最後一天,last_day(),利用它可以得到某個月有多少天

select last_day(now()),day(last_day(now()));
8、獲取某天位於一年中的第n周week(date,3)=weekofyear(),week()函式的第二個引數用來設定以星期幾做為一周的開始

select week(now(),3),weekofyear(now());
9、獲取兩個日期或者兩個時間的相差,datediff(),timediff()

select

datediff(curdate(),'

2018-02-15

'),timediff(curtime(),'

09:09:09

')

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

select time_to_sec(@d),sec_to_time(12389);
11、日期與天數的轉換,to_days(),from_days()

12、字串轉換為日期,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

');

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

select makedate(2015,200),maketime(13,20,15);

Mysql日期時間函式

1.獲取當前時刻時間 1.1返回當前時刻的日期和時間 1.2獲取當前時刻的日期 1.3獲取當前時刻的時間 1.4獲取當前時刻的週數 2.日期時間格式轉換 3.日期時間運算 3.1向後偏移時間 3.2向前偏移時間 3.3兩日期做差 獲取當前時刻的時間就是獲取程式執行的那一刻與時間相關的資料,比如年月日...

mysql 時間 mysql時間日期函式

now current timestamp 當前日期時間 current date 當前日期 current time 當前時間 date yyyy mm dd hh ii ss 獲取日期部分 time yyyy mm dd hh ii ss 獲取時間部分 date format yyyy mm d...

mysql日期函式 簡書 MySQL日期時間函式

日期時間函式 1 獲取子值,值為整數型別,函式如下 year date 返回date的年份 範圍在1000到9999 month date 返回date中的月份數值 day date 返回date中的日期數值 hour time 返回time的小時數 範圍是0到23 minute time 返回ti...