失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > MySql时间日期函数及格式化详解

MySql时间日期函数及格式化详解

时间:2022-10-08 22:16:45

相关推荐

MySql时间日期函数及格式化详解

前言

在我们的开发过程中,我们不可避免的需要接触到各种各样的数据类型,其中时间数据是最复杂多样的,为了满足客户的需求,我们需要定制不同的时间格式转换格式,有些我们会放到服务器程序上,通过代码实现时间日期的格式化,也可以通过MySql在查询时就转换成我们需要的格式,下面我们就具体了解一下MySql相关的时间日期的处理函数。

函数及格式化

1)获取当前日期及格式化输出

获取系统日期:now()

格式化日期:date_fformat(date,format)

返回系统日期。输出-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();

获取当前日期:curdate()

select curdate();

获取当前时间:curtime()

select curtime();

获取当前UTC日期时间函数:utc_date(),utc_time,utc_timestamp()

select utc_timestamp();

select utc_date();

select utc_time();

select now();

日期时间Extract函数

1)选取日期时间的各个部分:日期、时间、年、季度、月、日、小时、分钟、秒、微妙

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

select date(@dt); ---10-29

select time(@dt); --15:19:00.123456

select year(@dt); --

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

MySql Extract()函数

set @dt = '-09-10 07:15:30.123456' ;

select extract( year from @dt); --

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); -- 09

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

MySql dayof…函数:dayofweek(),dayofmonth(),dayofyear()

分别返回日期参数,在一周、一月、一年的位置

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

select dayofweek(@dt); --周几

select dayofmonth(@dt); --几月

select dayofyear(@dt); --某天

MySql week…函数:week(),weekofyear(),weekday(),yearweek()

set @dt='-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); --那年的第几周 43

weekday:(0 =Monday, 1 = Tuesday, …, 6 = Sunday);

dayofweek:(1 =Sunday, 2 = Monday,…, 7 = Saturday)

MySql 返回星期和月份名称函数:dayname(),monthname()

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

select dayname(@dt); --Thursday

select monthname(@dt); --October

MySQL last_day() 函数:返回月份中的最后一天

select last_day('-10-29 15:15:20'); ----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(08,2), period_add(0808,-2) --日期加/减去2月

4)MySQL 日期、时间相减函数:datediff(date1,date2), timediff(time1,time2)

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

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

MySQL 日期转换函数、时间转换函数

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'

MySQL (日期、天数)转换函数:to_days(date), from_days(days)

select to_days( '0000-00-00' ); -- 0

select to_days( '-08-08' ); -- 733627

select from_days(0); -- '0000-00-00'

select from_days(733627); -- '-08-08'

MySQL Str to Date (字符串转换为日期)函数:str_to_date(str, format)

select str_to_date( '08/09/' , '%m/%d/%Y' ); -- -08-09

MySQL 日期/时间转换为字符串函数:date_format(date,format), time_format(time,format)

select date_format( '-08-08 22:23:00' , '%W %M %Y' ); --Friday August

select date_format('-08-08 22:23:01', '%Y%m%d%H%i%s'); --0808222301

select time_format('22:23:01', '%H.%i.%s'); --22.23.01

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)函数

MySQL 获得当前时间戳函数:current_timestamp, current_timestamp()

select current_timestamp , current_timestamp ();

MySQL (Unix 时间戳、日期)转换函数

unix_timestamp(),

unix_timestamp( date ),

from_unixtime(unix_timestamp),

from_unixtime(unix_timestamp,format)

MySQL 时间戳(timestamp)转换、增、减函数:

timestamp ( date ) -- date to timestamp

timestamp (dt, time ) -- dt + time

timestampadd(unit,interval,datetime_expr) --

timestampdiff(unit,datetime_expr1,datetime_expr2) --

如果觉得《MySql时间日期函数及格式化详解》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。