1. 程式人生 > 其它 >mysql日期函式總結

mysql日期函式總結

1)獲取當前日期及格式化輸出
獲取系統日期:now()
格式化日期:date_fformat(date,format)
返回系統日期。輸出2009-12-25 14:00:00

select now(); #獲取當前時間
select date_format(now(),'%Y-%m-%d %H:%I:%s'); #獲取當前時間並格式化輸出

format可選格式型別:

%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 兩位數字表示的年份 %% 直接值“%” 除了now()外,MySql還提供了一下函式處理時間 current_timestamp() localtime() localtimestamp() 等同於now()

2)獲得當前日期+時間:sysdate()

select sysdate();

3)獲取當前日期:curdate()

select curdate();

4)獲取當前時間:curtime()

select curtime();

5)獲取當前UTC日期時間函式:utc_date(),utc_time,utc_timestamp()

select utc_timestamp();
select utc_date();
select utc_time();
select now();

MySQL 日期轉換函式、時間轉換函式

1)MySQL (時間、秒)轉換函式:time_to_sec(time),sec_to_time(seconds)

select time_to_sec( '01:00:05' ); -- 3605
select sec_to_time(3605); -- '01:00:05'

2)MySQL (日期、天數)轉換函式:to_days(date),from_days(days)

select to_days( '0000-00-00' ); -- 0
select to_days( '2008-08-08' ); -- 733627
select from_days(0); -- '0000-00-00'
select from_days(733627); -- '2008-08-08'

3)MySQL Str to Date (字串轉換為日期)函式:str_to_date(str, format)

select str_to_date( '08/09/2008' , '%m/%d/%Y' ); -- 2008-08-09
select str_to_date( '2022-02-28 00:00:00' , '%Y-%m-%d %H:%I:%s' ); -- 2022-02-28 00:00:00

4)MySQL 日期/時間轉換為字串函式:date_format(date,format),time_format(time,format)

select date_format( '2008-08-08 22:23:00' , '%W %M %Y' ); --Friday August 2008
select date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s'); --20080808222301
select time_format('22:23:01', '%H.%i.%s'); --22.23.01
select DATE_FORMAT(NOW(),'%Y-%m-%d %H-%I-%s');##日期格式化

5)MySQL 獲得國家地區時間格式函式:get_format()

--語法 :get_format(date|time|datetime, 'eur'|'usa'|'jis'|'iso'|'internal'

select get_format( date , 'usa' ) ; -- '%m.%d.%Y'
select get_format( date , 'jis' ) ; -- '%Y-%m-%d'
select get_format( date , 'iso' ) ; -- '%Y-%m-%d'
select get_format( date , 'eur' ) ; -- '%d.%m.%Y'
select get_format( date , 'internal' ) ; -- '%Y%m%d'
select get_format(datetime, 'usa' ) ; -- '%Y-%m-%d %H.%i.%s'
select get_format(datetime, 'jis' ) ; -- '%Y-%m-%d %H:%i:%s'
select get_format(datetime, 'iso' ) ; -- '%Y-%m-%d %H:%i:%s'
select get_format(datetime, 'eur' ) ; -- '%Y-%m-%d %H.%i.%s'
select get_format(datetime, 'internal' ) ; -- '%Y%m%d%H%i%s'
select get_format( time , 'usa' ) ; -- '%h:%i:%s %p'
select get_format( time , 'jis' ) ; -- '%H:%i:%s'
select get_format( time , 'iso' ) ; -- '%H:%i:%s'
select get_format( time , 'eur' ) ; -- '%H.%i.%s'
select get_format( time , 'internal' ) ; -- '%H%i%s'

6)MySQL 拼湊日期、時間函式:makdedate(year,dayofyear),maketime(hour,minute,second)

select makedate(2001,31); -- '2001-01-31'
select makedate(2001,32); -- '2001-02-01'
select maketime(12,15,30); -- '12:15:30'

MySQL 時間戳(Timestamp)函式

1)MySQL 獲得當前時間戳函式:current_timestamp,current_timestamp()

select current_timestamp , current_timestamp ();

2)MySQL (Unix 時間戳、日期)轉換函式

unix_timestamp(),
unix_timestamp( date ),
from_unixtime(unix_timestamp),
from_unixtime(unix_timestamp,format)

3)MySQL 時間戳(timestamp)轉換、增、減函式

timestamp ( date ) -- date to timestamp
timestamp (dt, time ) -- dt + time
timestampadd(unit,interval,datetime_expr) --
timestampdiff(unit,datetime_expr1,datetime_expr2) --

日期時間Extract函式

1)選取日期時間的各個部分:日期、時間、年、季度、月、日、小時、分鐘、秒、微妙

set @dt = '2020-10-29 15:19:00.123456'

select date(@dt); --2020-10-29
select time(@dt); --15:19:00.123456
select year(@dt); --2020
select quarter(@dt); --4
select month(@dt); --10
select week(@dt); --43
select day(@dt); --29
select hour(@dt); --15
select minute(@dt); --19
select second(@dt); --00
select microsecond(@dt); --123456

2)MySqlExtract()函式

set @dt = '2008-09-10 07:15:30.123456' ;
 
select extract( year from @dt); -- 2008
select extract(quarter from @dt); -- 3
select extract( month from @dt); -- 9
select extract(week from @dt); -- 36
select extract( day from @dt); -- 10
select extract( hour from @dt); -- 7
select extract( minute from @dt); -- 15
select extract( second from @dt); -- 30
select extract(microsecond from @dt); -- 123456
select extract(year_month from @dt); -- 200809
select extract(day_hour from @dt); -- 1007
select extract(day_minute from @dt); -- 100715
select extract(day_second from @dt); -- 10071530
select extract(day_microsecond from @dt); -- 10071530123456
select extract(hour_minute from @dt); -- 715
select extract(hour_second from @dt); -- 71530
select extract(hour_microsecond from @dt); -- 71530123456
select extract(minute_second from @dt); -- 1530
select extract(minute_microsecond from @dt); -- 1530123456
select extract(second_microsecond from @dt); -- 30123456

3)MySql dayof…函式:dayofweek(),dayofmonth(),dayofyear()
分別返回日期引數,在一週、一月、一年的位置

set @dt='2020-10-29 15:15:20'

select dayofweek(@dt); --周幾
select dayofmonth(@dt);  --幾月
select dayofyear(@dt);  --某天

4)MySql week…函式:week(),weekofyear(),weekday(),yearweek()

set @dt='2020-10-29 15:15:20'

select week(@dt); --第幾周
select week(@dt,3);  --三天後是第幾周
select weekofyear(@dt);  --一年中的第幾周
select dayofweek(@dt);  --“某天”在一週中的位置
select weekday(@dt);  --3
select yearweek(@dt);  --那年的第幾周  202043
weekday:(0 =Monday, 1 = Tuesday, …, 6 = Sunday);
dayofweek:(1 =Sunday, 2 = Monday,…, 7 = Saturday)

5)MySql 返回星期和月份名稱函式:dayname(),monthname()

set @dt='2020-10-29 15:15:20'

select dayname(@dt);  --Thursday
select monthname(@dt);  --October

6)MySQLlast_day()函式:返回月份中的最後一天

select last_day('2020-10-29 15:15:20'); ---2020-10-31
select day (last_day(now())) as days; --獲取本月有多少天

MySql日期時間計算函式

1)MySql 為日期增加一個時間間隔:date_add()

set @dt=now();

select date_add(@dt, interval 1 day ); -- 加一天
select date_add(@dt, interval 1 hour ); -- 加一小時
select date_add(@dt, interval 1 minute ); -- 加一分
select date_add(@dt, interval 1 second ); --加一秒
select date_add(@dt, interval 1 microsecond); --加一毫秒
select date_add(@dt, interval 1 week); --加一週
select date_add(@dt, interval 1 month ); --加一月
select date_add(@dt, interval 1 quarter); --加一季度
select date_add(@dt, interval 1 year );  --加一年
select date_add(@dt, interval -1 day ); -- 減一天

select date_add(@dt, interval '01:15:30' hour_second); --加01:15:30
select date_add(@dt, interval '1 01:15:30' day_second); --加1天01:15:30

2)MySQL 為日期減去一個時間間隔:date_sub()

set @dt=now();

select date_sub(@dt, interval '1 1:1:1' day_second); --減去1天1:1:1

3)MySQL 另類日期函式:period_add(P,N),period_diff(P1,P2)

-- 函式引數“P” 的格式為“YYYYMM” 或者 “YYMM”,第二個引數“N” 表示增加或減去 N month(月)
select period_add(200808,2), period_add(20080808,-2) --日期加/減去2月

4)MySQL 日期、時間相減函式:datediff(date1,date2),timediff(time1,time2)

select datediff( '2008-08-08' , '2008-08-01' ); -- 7 返回天數

select timediff( '2008-08-08 08:08:08' , '2008-08-08 00:00:00' ); -- 08:08:08 返回 time 差值