当前位置:   article > 正文

MySQL 5.6常用日期函数_mysql 5.6 current_date()

mysql 5.6 current_date()

1、ADDDATE 添加时间间隔

用法:

ADDDATE(date,INTERVAL expr unit)

ADDDATE(expr,days)

 

  1. -- 增加时间间隔
  2. select ADDDATE(NOW(),INTERVAL 1 YEAR) aa,ADDDATE('2014-1-1 01:01:01',INTERVAL 1 YEAR) bb;
  3. select ADDDATE(NOW(),INTERVAL 1 MONTH) aa,ADDDATE('2014-1-1 01:01:01',INTERVAL 1 MONTH) bb;
  4. select ADDDATE(NOW(),INTERVAL 1 WEEK) aa,ADDDATE('2014-1-1 01:01:01',INTERVAL 1 WEEK) bb;
  5. select ADDDATE(NOW(),INTERVAL 1 DAY) aa,ADDDATE('2014-1-1 01:01:01',INTERVAL 1 DAY) bb;
  6. select ADDDATE(NOW(),INTERVAL 100 HOUR) aa,ADDDATE('2014-1-1 01:01:01',INTERVAL 100 HOUR) bb;
  7. select ADDDATE(NOW(),INTERVAL 100 MINUTE) aa,ADDDATE('2014-1-1 01:01:01',INTERVAL 100 MINUTE) bb;
  8. select ADDDATE(now(),INTERVAL '0001-01' YEAR_MONTH) aa,ADDDATE('2014-1-1 01:01:01',INTERVAL '0001-01' YEAR_MONTH) bb;
  9. select ADDDATE(NOW(),INTERVAL '1:10:10' HOUR_SECOND) aa,ADDDATE('2014-1-1 01:01:01',INTERVAL '1:10:10' HOUR_SECOND) bb;
  10. -- 增加天数
  11. SELECT ADDDATE(NOW(), 31) aa,ADDDATE('2008-01-02', 31) bb;

官方文档时间间隔定义:

unit ValueExpected expr Format
MICROSECONDMICROSECONDS
SECONDSECONDS
MINUTEMINUTES
HOURHOURS
DAYDAYS
WEEKWEEKS
MONTHMONTHS
QUARTERQUARTERS
YEARYEARS
SECOND_MICROSECOND'SECONDS.MICROSECONDS'
MINUTE_MICROSECOND'MINUTES:SECONDS.MICROSECONDS'
MINUTE_SECOND'MINUTES:SECONDS'
HOUR_MICROSECOND'HOURS:MINUTES:SECONDS.MICROSECONDS'
HOUR_SECOND'HOURS:MINUTES:SECONDS'
HOUR_MINUTE'HOURS:MINUTES'
DAY_MICROSECOND'DAYS HOURS:MINUTES:SECONDS.MICROSECONDS'
DAY_SECOND'DAYS HOURS:MINUTES:SECONDS'
DAY_MINUTE'DAYS HOURS:MINUTES'
DAY_HOUR'DAYS HOURS'
YEAR_MONTH'YEARS-MONTHS'


2、ADDTIME 添加时间

ADDTIME(expr1,expr2)

 

  1. -- 增加时间
  2. select ADDTIME(NOW(), '30:0:0') aa,ADDTIME('10:10:10', '30:0:0') bb;
  3. select ADDTIME(NOW(), '1 1:1:1.000002') aa,ADDTIME('10:10:10', '1 1:1:1.000002') bb;
  4. select ADDTIME(NOW(), '100 01:01:01.000002') aa,ADDTIME('10:10:10', '100 01:01:01.000002') bb;


3、CONVERT_TZ 时区时间转换

 

CONVERT_TZ(dt,from_tz,to_tz)

 

  1. -- 0时区 -> 东八区
  2. SELECT CONVERT_TZ('2014-01-01 01:00:00','+00:00','+8:00') aa;
  3. -- 西八区 -> 东八区
  4. SELECT CONVERT_TZ('2014-01-01 01:00:00','-8:00','+8:00') aa;


4、当前时间 CURRENT_DATE

  1. -- 当前日期
  2. select CURRENT_DATE() a1,CURRENT_DATE a2,CURDATE() a3;
  3. -- 当前时间
  4. select CURRENT_TIME() a1,CURRENT_TIME a2,CURTIME() a3;
  5. -- 当前时间戳
  6. select CURRENT_TIMESTAMP() a1,CURRENT_TIMESTAMP a2,NOW() a3;
  7. select NOW() a1,LOCALTIME a2,LOCALTIME() a3,LOCALTIMESTAMP a4,LOCALTIMESTAMP () a5;

 

 

 

5、增加减去时间间隔DATE_ADD, DATE_SUB(SUBDATE)

DATE_ADD(date,INTERVAL expr unit)DATE_SUB(date,INTERVAL expr unit)

  1. -- 时间加减
  2. select date_add(NOW(),INTERVAL 1 YEAR) aa,date_add('2014-1-1 01:01:01',INTERVAL 1 YEAR) bb;
  3. select date_add(NOW(),INTERVAL 1 MONTH) aa,date_add('2014-1-1 01:01:01',INTERVAL 1 MONTH) bb;
  4. select date_add(NOW(),INTERVAL 1 WEEK) aa,date_add('2014-1-1 01:01:01',INTERVAL 1 WEEK) bb;
  5. select date_add(NOW(),INTERVAL 1 DAY) aa,date_add('2014-1-1 01:01:01',INTERVAL 1 DAY) bb;
  6. select date_add(NOW(),INTERVAL 100 HOUR) aa,date_add('2014-1-1 01:01:01',INTERVAL 100 HOUR) bb;
  7. select date_add(NOW(),INTERVAL 100 MINUTE) aa,date_add('2014-1-1 01:01:01',INTERVAL 100 MINUTE) bb;
  8. select date_add(now(),INTERVAL '0001-01' YEAR_MONTH) aa,date_add('2014-1-1 01:01:01',INTERVAL '0001-01' YEAR_MONTH) bb;
  9. select date_add(NOW(),INTERVAL '1:10:10' HOUR_SECOND) aa,date_add('2014-1-1 01:01:01',INTERVAL '1:10:10' HOUR_SECOND) bb;
  10. select DATE_SUB(NOW(),INTERVAL 1 YEAR) aa,DATE_SUB('2014-1-1 01:01:01',INTERVAL 1 YEAR) bb;
  11. select DATE_SUB(NOW(),INTERVAL 1 MONTH) aa,DATE_SUB('2014-1-1 01:01:01',INTERVAL 1 MONTH) bb;
  12. select DATE_SUB(NOW(),INTERVAL 1 WEEK) aa,DATE_SUB('2014-1-1 01:01:01',INTERVAL 1 WEEK) bb;
  13. select DATE_SUB(NOW(),INTERVAL 1 DAY) aa,DATE_SUB('2014-1-1 01:01:01',INTERVAL 1 DAY) bb;
  14. select DATE_SUB(NOW(),INTERVAL 100 HOUR) aa,DATE_SUB('2014-1-1 01:01:01',INTERVAL 100 HOUR) bb;
  15. select DATE_SUB(NOW(),INTERVAL 100 MINUTE) aa,DATE_SUB('2014-1-1 01:01:01',INTERVAL 100 MINUTE) bb;
  16. select DATE_SUB(now(),INTERVAL '0001-01' YEAR_MONTH) aa,DATE_SUB('2014-1-1 01:01:01',INTERVAL '0001-01' YEAR_MONTH) bb;
  17. select DATE_SUB(NOW(),INTERVAL '1:10:10' HOUR_SECOND) aa,DATE_SUB('2014-1-1 01:01:01',INTERVAL '1:10:10' HOUR_SECOND) bb;


6、日期格式化DATE_FORMAT(date,format)

官方定义参数:

SpecifierDescription
%aAbbreviated weekday name (Sun..Sat)
%bAbbreviated month name (Jan..Dec)
%cMonth, numeric (0..12)
%DDay of the month with English suffix (0th1st2nd3rd, …)
%dDay of the month, numeric (00..31)
%eDay of the month, numeric (0..31)
%fMicroseconds (000000..999999)
%HHour (00..23)
%hHour (01..12)
%IHour (01..12)
%iMinutes, numeric (00..59)
%jDay of year (001..366)
%kHour (0..23)
%lHour (1..12)
%MMonth name (January..December)
%mMonth, numeric (00..12)
%pAM or PM
%rTime, 12-hour (hh:mm:ss followed by AM or PM)
%SSeconds (00..59)
%sSeconds (00..59)
%TTime, 24-hour (hh:mm:ss)
%UWeek (00..53), where Sunday is the first day of the week
%uWeek (00..53), where Monday is the first day of the week
%VWeek (01..53), where Sunday is the first day of the week; used with %X
%vWeek (01..53), where Monday is the first day of the week; used with %x
%WWeekday name (Sunday..Saturday)
%wDay of the week (0=Sunday..6=Saturday)
%XYear for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%xYear for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%YYear, numeric, four digits
%yYear, numeric (two digits)
%%A literal “%” character
%xx, for any “x” not listed above
  1. -- 日期格式化
  2. SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s') aa;
  3. SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s %W') aa;


7、获取日期部分数据

  1. -- 日期部分获取
  2. select DATE(NOW()) aa;
  3. select TIME(NOW()) aa;
  4. select YEAR(NOW()) aa;
  5. select QUARTER(NOW()) aa;
  6. select MONTH(NOW()) aa,MONTHNAME(NOW()) bb;
  7. select DAY(NOW()) aa;
  8. select HOUR(NOW()) aa;
  9. select MINUTE(NOW()) aa;
  10. select SECOND(NOW()) aa;
  11. select MICROSECOND(NOW()) aa;
  12. select WEEKDAY(NOW()) aa;
  13. select WEEKOFYEAR(NOW()) aa;
  14. select YEARWEEK(NOW()) aa;
  15. select DAYNAME(NOW()) aa;
  16. select DAYOFMONTH(NOW()) aa;
  17. select DAYOFWEEK(NOW()) aa;
  18. select DAYOFYEAR(NOW()) aa;


8、DATEDIFF,TIMEDIFF 日期、时间相减

  1. -- 两个时间相减(日期部分相减,返回天数)
  2. select DATEDIFF('2014-1-1 10:00:00','2014-2-2 01:00:00') aa;
  3. select TIMEDIFF('2014-1-1 10:10:10','2014-2-2 01:20:30') aa;
  4. SELECT TIMESTAMPDIFF(YEAR,'2003-02-01','2003-05-01') aa;
  5. SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01') aa;
  6. SELECT TIMESTAMPDIFF(DAY,'2003-02-01','2003-05-01');
  7. SELECT TIMESTAMPDIFF(HOUR,'2003-02-01','2003-05-01');


9、EXTRACT(unit FROM date)提取日期数据

  1. -- 提取日期数据
  2. select EXTRACT(YEAR FROM '2014-1-1') aa;
  3. select EXTRACT(MONTH FROM NOW()) aa;
  4. select EXTRACT(DAY FROM NOW()) aa;
  5. select EXTRACT(WEEK FROM NOW()) aa;


10、从数字数据获取时间

  1. -- 从数字数据获取时间
  2. SELECT FROM_DAYS(730669.89) aa;
  3. SELECT FROM_UNIXTIME(1196440219.12) aa;
  4. SELECT FROM_UNIXTIME(1196440219) + 0 aa;
  5. SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),'%Y %D %M %h:%i:%s %x');


11、发挥格式化日期字符串

官方定义的几种格式:

  1. SELECT DATE_FORMAT(NOW(),GET_FORMAT(DATE,'EUR'));
  2. SELECT DATE_FORMAT(NOW(),GET_FORMAT(DATE,'USA'));
  3. SELECT DATE_FORMAT(NOW(),GET_FORMAT(DATE,'ISO'));


12、LAST_DAY获取当前月份的最后一天

  1. SELECT LAST_DAY('2014-02-05') aa;
  2. SELECT LAST_DAY('2014-02-05 11:11:11') aa;
  3. SELECT LAST_DAY(NOW()) aa;


13、创建日期、时间MAKEDATE,MAKETIME

MAKEDATE(year,dayofyear)

MAKETIME(hour,minute,second)

  1. select MAKEDATE(2014,10) aa;
  2. select MAKETIME(13,1,6) aa;


14、增加月份PERIOD_ADD(P,N)

Adds N months to period P (in the format YYMM or YYYYMM)

  1. select PERIOD_ADD('201401',3) aa;
  2. select PERIOD_ADD('1401',3) aa;

 

15、计算月份差 PERIOD_DIFF(P1,P2)

P1,P2 (in the format YYMM or YYYYMM)

  1. SELECT PERIOD_DIFF(201402,201403) aa;
  2. SELECT PERIOD_DIFF('201402','201403') aa;


16、秒换算时间SEC_TO_TIME(seconds)

TIME_TO_SEC(time)

  1. select SEC_TO_TIME(60) aa;
  2. select SEC_TO_TIME(3600) aa;
  3. select TIME_TO_SEC(current_time) aa;
  4. select TIME_TO_SEC('00:10:10') aa;


17、字符串转日期STR_TO_DATE(str,format)

 

  1. -- 格式必须要匹配
  2. SELECT STR_TO_DATE('01,5,2013','%d,%m,%Y') aa;
  3. SELECT STR_TO_DATE('2014-10-10','%Y-%m-%D') aa;
  4. SELECT STR_TO_DATE('2014-10-10','%d,%m,%Y') aa;


18、时间格式化 TIME_FORMAT(time,format)

  1. select TIME_FORMAT('08:08:01','%h:%i:%s') aa;
  2. select TIME_FORMAT(current_time,'%H:%m:%s') aa;


19、时间戳加减

TIMESTAMPADD(unit,interval,datetime_expr)

TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)

  1. SELECT TIMESTAMPADD(YEAR,1,'2014-01-02') aa;
  2. SELECT TIMESTAMPADD(MONTH,1,'2014-01-02') aa;
  3. SELECT TIMESTAMPADD(DAY,1,'2014-01-02') aa;
  4. SELECT TIMESTAMPDIFF(YEAR,NOW(),'2014-01-02') aa;
  5. SELECT TIMESTAMPDIFF(MONTH,NOW(),'2014-01-02') aa;
  6. SELECT TIMESTAMPDIFF(DAY,NOW(),'2014-01-02') aa;


20、UTC时间获取

select UTC_DATE() a1,UTC_TIME() a2,UTC_TIMESTAMP() a3;

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/406720
推荐阅读
相关标签
  

闽ICP备14008679号

        
cppcmd=keepalive&