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-dd hh:ii:ss', '%d %y %a %d %m %b %j');    -- 格式化時間
unix_timestamp();                -- 獲得unix時間戳
from_unixtime();                -- 從時間戳獲得時間

官網:https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_yearweek

 select now(),current_date,current_time;

格式:

查詢每個月1號

select * from lagouok where right(date_format([字段],'%Y-%m-%d'),2)=1

獲取星期幾

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

mysql根據生日查詢年齡

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

查詢多少年前select date_sub(sysdate(),interval 10 year)

 

 

date_format([字段名稱],'%Y-%m-%d') = curdate()

 

 

1、當前日期

select DATE_SUB(curdate(),INTERVAL 0 DAY) ;


2、明天日期
select DATE_SUB(curdate(),INTERVAL -1 DAY) ;


3、昨天日期

select adddate(now(),-1)
select date_sub(now(),interval 1 day)

 

 

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  兩位數字表示的年份
%%  直接值“%”


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM