当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL日期函数与日期转换格式化函数大全

MySQL日期函数与日期转换格式化函数大全

2018年03月08日  | 移动技术网IT编程  | 我要评论

mysql作为一款开元的免费关系型数据库,用户基础非常庞大,本文列出了mysql常用日期函数与日期转换格式化函数

1、dayofweek(date)

select dayofweek(‘2016-01-16') 
select dayofweek(‘2016-01-16 00:00:00')
-> 7 (表示返回日期date是星期几,记住:星期天=1,星期一=2, ... 星期六=7)

2、weekday(date)

select weekday(‘2016-01-16') 
select weekday(‘2016-01-16 00:00:00')

 

-> 5 (表示返回date是在一周中的序号,西方日历中通常一周的开始是星期天,并且以0开始计数,所以,记住:0=星期一,1=星期二, ... 5=星期六)

3、dayofmonth(date)

select dayofmonth(‘2016-01-16') 
select dayofmonth(‘2016-01-16 00:00:00')
-> 16 (表示返回date是当月的第几天,1号就返回1,... ,31号就返回31)

4、dayofyear(date)

select dayofyear(‘2016-03-31') 
select dayofyear(‘2016-03-31 00:00:00')
-> 91 (表示返回date是当年的第几天,01.01返回1,... ,12.31就返回365)

5、month(date)

select month(‘2016-01-16') 
select month(‘2016-01-16 00:00:00')
-> 1 (表示返回date是当年的第几月,1月就返回1,... ,12月就返回12)

6、dayname(date)

select dayname(‘2016-01-16') 
select dayname(‘2016-01-16 00:00:00')
-> saturday (表示返回date是周几的英文全称名字)

7、monthname(date)

select monthname(‘2016-01-16') 
select monthname(‘2016-01-16 00:00:00')
-> january (表示返回date的是当年第几月的英文名字)

8、quarter(date)

select quarter(‘2016-01-16') 
select quarter(‘2016-01-16 00:00:00')
-> 1 (表示返回date的是当年的第几个季度,返回1,2,3,4)

9、week(date,index)

select week(‘2016-01-03') 
select week(‘2016-01-03', 0) 
select week(‘2016-01-03', 1)
-> 1 (该函数返回date在一年当中的第几周,date(01.03)是周日,默认是以为周日作为一周的第一天,函数在此处返回1可以有两种理解:1、第一周返回0,第二周返回1,.... ,2、以当年的完整周开始计数,第一周返回1,第二周返回2,... ,最后一周返回53)
-> 1 (week()默认index就是0. 所以结果同上)
-> 0 (当index为1时,表示一周的第一天是周一,所以,4号周一才是第二周的开始日)

10、year(date)

select year(‘70-01-16') 
select year(‘2070-01-16') 
select year(‘69-01-16 00:00:00')
-> 1970 (表示返回date的4位数年份)
-> 2070 
-> 1969 

要注意的是:如果年份只有两位数,那么自动补全的机制是以默认时间1970.01.01为界限的,>= 70 的补全 19,< 70 的补全 20

11、hour(time)

select hour(‘11:22:33') 
select hour(‘2016-01-16 11:22:33')
-> 11
-> 11

返回该date或者time的hour值,值范围(0-23)

12、minute(time)

select minute(‘11:22:33') 
select minute(‘2016-01-16 11:44:33')
-> 22
-> 44

返回该time的minute值,值范围(0-59)

13、second(time)

select second(‘11:22:33') 
select second(‘2016-01-16 11:44:22')
-> 33
-> 22

返回该time的minute值,值范围(0-59)

14、period_add(month,add)

select period_add(1601,2) 
select period_add(191602,3) 
select period_add(191602,-3)
-> 201603
-> 191605
-> 191511

该函数返回对month做增减的操作结果,month的格式为yymm或者yyyymm,返回的都是yyyymm格式的结果,add可以传负值

15、period_diff(monthstart,monthend)

select period_diff(1601,1603) 
select period_diff(191602,191607) 
select period_diff(1916-02,1916-07) 
select period_diff(1602,9002)
-> -2
-> -5
-> 5
-> 312

该函数返回monthstart - monthend的间隔月数

16、date_add(date,interval number type),同 adddate()

select date_add(“2015-12-31 23:59:59”,interval 1 second) 
select date_add(“2015-12-31 23:59:59”,interval 1 day) 
select date_add(“2015-12-31 23:59:59”,interval “1:1” minute_second) 
select date_add(“2016-01-01 00:00:00”,interval “-1 10” day_hour)
-> 2016-01-01 00:00:00
-> 2016-01-01 23:59:59
-> 2016-01-01 00:01:00
-> 2015-12-30 14:00:00

date_add()和adddate()返回对date操作的结果

1、date的格式可以是“15-12-31”,可以是“15-12-31 23:59:59”,也可以是“2015-12-31 23:59:59”,如果参数date是date格式,则返回date格式结果,如果参数date是datetime格式,则返回datetime格式结果

2、type格式:
    second 秒 seconds
    minute 分钟 minutes
    hour 时间 hours
    day 天 days
    month 月 months
    year 年 years
    minute_second 分钟和秒 "minutes:seconds"
    hour_minute 小时和分钟 "hours:minutes"
    day_hour 天和小时 "days hours"
    year_month 年和月 "years-months"
    hour_second 小时, 分钟, "hours:minutes:seconds"
    day_minute 天, 小时, 分钟 "days hours:minutes"
    day_second 天, 小时, 分钟, 秒 "days hours:minutes:seconds"


3、另外,如果不用函数,也可以考虑用操作符“+”,“-”,例子如下:

select “2016-01-01” - interval 1 second 
select “2016-01-01” - interval 1 day 
select ‘2016-12-31 23:59:59' + interval 1 second 
select ‘2016-12-31 23:59:59' + interval “1:1” minute_second

返回结果:

-> 2015-12-31 23:59:59
-> 2015-12-31
-> 2017-01-01 00:00:00
-> 2017-01-01 00:01:00

17、date_sub(date,interval number type),同 subdate()

用法和date_add()与adddate()类似,一个是加,一个是减,用时参照16点,具体用法请参考date_add()与adddate()。

18、to_days(date)

select to_days(‘2016-01-16') 
select to_days(‘20160116') 
select to_days(‘160116')
-> 736344
-> 736344
-> 736344

返回西元0年至日期date是总共多少天

19、from_days(date)

select from_days(367)
-> 0001-01-02

返回西元0年至今多少天的date值

20、date_format(date,format):根据参数对date进行格式化。

select date_format(‘2016-01-16 22:23:00','%w %m %y') 
select date_format(‘2016-01-16 22:23:00','%d %y %a %d %m %b %j') 
select date_format(‘2016-01-16 22:23:00','%h %k %i %r %t %s %w') 
select date_format(‘2016-01-16 22:23:00','%y-%m-%d %h:%i:%s')
-> saturday january 2016
-> 16th 16 sat 16 01 jan 016
-> 22 22 10 10:23:00 pm 22:23:00 00 6
-> 2016-01-16 22:23:00

format的格式都列出来:

    %m 月名字(january……december)
    %w 星期名字(sunday……saturday)
    %d 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
    %y 年, 数字, 4 位
    %y 年, 数字, 2 位
    %a 缩写的星期名字(sun……sat)
    %d 月份中的天数, 数字(00……31)
    %e 月份中的天数, 数字(0……31)
    %m 月, 数字(01……12)
    %c 月, 数字(1……12)
    %b 缩写的月份名字(jan……dec)
    %j 一年中的天数(001……366)
    %h 小时(00……23)
    %k 小时(0……23)
    %h 小时(01……12)
    %i 小时(01……12)
    %l 小时(1……12)
    %i 分钟, 数字(00……59)
    %r 时间,12 小时(hh:mm:ss [ap]m)
    %t 时间,24 小时(hh:mm:ss)
    %s 秒(00……59)
    %s 秒(00……59)
    %p am或pm
    %w 一个星期中的天数(0=sunday ……6=saturday )
    %u 星期(0……52), 这里星期天是星期的第一天
    %u 星期(0……52), 这里星期一是星期的第一天
    %% 字符% )

time_format(time,format):
具体用法和date_format()类似,但time_format只处理小时、分钟和秒(其余符号产生一个null值或0)

21、获取系统当前日期

select curdate() 
select current_date()
-> 2016-01-16
-> 2016-01-16

22、获取系统当前时间

select curtime() 
select current_time()
-> 17:44:22
-> 17:44:22

23、now(),sysdate(),current_timestamp(),localtime():获取系统当前日期和时间

select now() 
select sysdate() 
select current_timestamp() 
select current_timestamp 
select localtime() 
select localtime
-> 2016-01-16 17:44:41
-> 2016-01-16 17:44:41
-> 2016-01-16 17:44:41
-> 2016-01-16 17:44:41
-> 2016-01-16 17:44:41
-> 2016-01-16 17:44:41

24、unix_timestamp(date):获取时间戳

select unix_timestamp() 
select unix_timestamp(‘2016-01-16') 
select unix_timestamp(‘2016-01-16 23:59:59')
-> 1452937627
-> 1452873600
-> 1452959999

25、from_unixtime(unix_timestamp,format):把时间戳转化成日期时间

select from_unixtime(1452959999) 
select from_unixtime(1452959999,'%y-%m-%d %h:%i:%s')
-> 2016-01-16 23:59:59
-> 2016-01-16 23:59:59

26、sec_to_time(seconds):把秒数转化成时间

select sec_to_time(2378)
-> 00:39:38

27、time_to_sec(time):把时间转化成秒数

select time_to_sec(‘22:23:00')
-> 2378

28、addtime(time,times):把times加到time上

select addtime(“2015-12-31 23:59:59”,'01:01:01')
-> 2016-01-01 01:01:00

29、convert_tz(date,from_tz ,to_tz ):转换时区

select convert_tz(‘2004-01-01 12:00:00','+00:00','+10:00')
-> 2004-01-01 22:00:00

30、str_to_date(date,format ):将字符串转成format格式的日期时间

select str_to_date(‘2015-01-01', ‘%y-%m-%d')
-> 2015-01-01

31、last_day(date ):获取date当月最后一天的日期

select last_day(sysdate()) 
select last_day(‘2015-02-02') 
select last_day(‘2015-02-02 00:22:33')
-> 2016-01-31
-> 2015-02-28
-> 2015-02-28

32、makedate(year ,dayofyear ):根据参数(年份,第多少天)获取日期

select makedate(2015 ,32)
-> 2015-02-01

33、 maketime(hour ,minute ,second ):根据参数(时,分,秒)获取时间

select maketime(12 ,23 ,34 )
-> 12:23:34

34、yearweek(date):获取日期的年和周

select yearweek(sysdate()) 
select yearweek(‘2015-01-10') 
select yearweek(‘2015-01-10',1)
-> 201602
-> 201501
-> 201502

35、weekofyear(date):获取当日是当年的第几周

select weekofyear(sysdate()) 
select weekofyear(‘2015-01-10')
-> 2
-> 2

-> 2
-> 2

mysql中常用的几种时间格式转换函数整理如下

1,from_unixtime(timestamp, format):

timestamp为int型时间,如14290450779;format为转换的格式,包含格式如下:

%m 月名字(january……december) 
%w 星期名字(sunday……saturday) 
%d 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。) 
%y 年, 数字, 4 位 
%y 年, 数字, 2 位 
%a 缩写的星期名字(sun……sat) 
%d 月份中的天数, 数字(00……31) 
%e 月份中的天数, 数字(0……31) 
%m 月, 数字(01……12) 
%c 月, 数字(1……12) 
%b 缩写的月份名字(jan……dec) 
%j 一年中的天数(001……366) 
%h 小时(00……23) 
%k 小时(0……23) 
%h 小时(01……12) 
%i 小时(01……12) 
%l 小时(1……12) 
%i 分钟, 数字(00……59) 
%r 时间,12 小时(hh:mm:ss [ap]m) 
%t 时间,24 小时(hh:mm:ss) 
%s 秒(00……59) 
%s 秒(00……59) 
%p am或pm 
%w 一个星期中的天数(0=sunday ……6=saturday ) 
%u 星期(0……52), 这里星期天是星期的第一天 
%u 星期(0……52), 这里星期一是星期的第一 

2,unix_timestamp(date):

作用与from_unixtime()刚好相反,前者是把unix时间戳转换为可读的时间,而unix_timestamp()是把可读的时间转换为unix时间戳,这在对datetime存储的时间进行排序时会用到。如unix_timestamp('2009-08-06 10:10:40'),得到1249524739。

如果unix_timestamp()不传参数,则调用now()函数自动取当前时间。

3,date_format(date, format):

date_format()是将date或datetime类型值转换为任意的时间格式。比如常见的应用场景,某表有一个字段是更新时间,存储的是datetime类型,但前台展示时只需要显示年月日(xxxx-xx-xx),这个时候就可以用date_format(date,'%y-%m-%d ')处理,而不需要在结果集中用程序循环处理。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网