mysql時間日期函式

2022-06-30 04:18:10 字數 2865 閱讀 8885

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-dd hh:ii:ss

', '

%d %y %a %d %m %b %j

'); --

格式化時間

unix_timestamp(); --

獲得unix時間戳

from_unixtime(); --

從時間戳獲得時間

right(date_format([字段],'

%y-%m-%d

'),2)=

1

select dayofweek(now())-

1

dayofweek(字段)=1  查詢每個月星期二

select

year(curdate())-

year(字段)-(right(curdate(),5)(字段,5)) 年齡 from emp;

date_format([欄位名稱],'%y-%m-%d') = curdate()

1、當前日期

select date_sub(curdate(),interval 0 day) ;

2、明天日期

select date_sub(curdate(),interval -1 day) ;

3、昨天日期

4、前乙個小時時間

select date_sub(now(), interval 1 hour);

5、後乙個小時時間

select date_sub(now(), interval -1 hour);

6、前30分鐘時間

select date_add(now(),interval -30 minute)

7、後30分鐘時間

select date_add(now(),interval 30 minute)

8、根據format字串格式化date值:

%s, %s  兩位數字形式的秒( 00,01, ..., 59)

%i, %i  兩位數字形式的分( 00,01, ..., 59)

%h  兩位數字形式的小時,24 小時(00,01, ..., 23)

%h  兩位數字形式的小時,12 小時(01,02, ..., 12)

%k  數字形式的小時,24 小時(0,1, ..., 23)

%l  數字形式的小時,12 小時(1, 2, ..., 12)

%t  24 小時的時間形式(hh:mm:ss)

%r  12 小時的時間形式(hh:mm:ss am 或hh:mm:ss pm)

%p  am或pm

%w  一周中每一天的名稱(sunday, monday, ..., saturday)

%a  一周中每一天名稱的縮寫(sun, mon, ..., sat)

%d  兩位數字表示月中的天數(00, 01,..., 31)

%e  數字形式表示月中的天數(1, 2, ..., 31)

%d  英文本尾表示月中的天數(1st, 2nd, 3rd,...)

%w  以數字形式表示週中的天數( 0 = sunday, 1=monday, ..., 6=saturday)

%j  以三位數字表示年中的天數( 001, 002, ..., 366)

%u  周(0, 1, 52),其中sunday 為週中的第一天

%u  周(0, 1, 52),其中monday 為週中的第一天

%m  月名(january, february, ..., december)

%b  縮寫的月名( january, february,...., december)

%m  兩位數字表示的月份(01, 02, ..., 12)

%c  數字表示的月份(1, 2, ...., 12)

%y  四位數字表示的年份

%y  兩位數字表示的年份

%%  直接值「%」

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 時間日期函式

dayofweek date dayofweek 返回日期date是星期幾 1 星期天,2 星期一,7 星期六,odbc標準 mysql select dayofweek 1992 03 03 3 weekday date weekday 返回日期date是星期幾 0 星期一,1 星期二,6 星期天...

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...