MySQL数据库中的日期相关函数整理

获得当前日期、时间函数

  • 返回当前日期(date)——————-函数:curdate()和current_date()
mysql> select curdate(); +------------+
| curdate() | +------------+
| 2015-08-06 | +------------+
1 row in set (0.00 sec)
mysql> select current_date(); +----------------+
| current_date() | +----------------+
| 2015-08-06 | +----------------+
1 row in set (0.00 sec)
  • 返回当前时间(time)———————-函数:curtime()和current_time()
mysql> select curtime(); +-----------+
| curtime() | +-----------+
| 12:01:48 | +-----------+
1 row in set (0.00 sec)
mysql> select current_time(); +----------------+
| current_time() | +----------------+
| 12:01:32 | +----------------+
1 row in set (0.00 sec)
  • 获得当前日期时间(date+time)———-函数:now()和sysdate()
mysql> select now(); +---------------------+
| now() | +---------------------+
| 2015-08-06 11:26:49 | +---------------------+
1 row in set (0.00 sec)

mysql> select sysdate(); +---------------------+
| sysdate() | +---------------------+
| 2015-08-06 11:26:55 | +---------------------+
1 row in set (0.00 sec)

注:now()在语句开始执行时值就得到了, 而sysdate()是在语句执行时动态得到值,具体实例如下:

mysql> select now(), sleep(3), now(); +---------------------+----------+---------------------+
| now() | sleep(3) | now() | +---------------------+----------+---------------------+
| 2015-08-06 11:32:17 | 0 | 2015-08-06 11:32:17 | +---------------------+----------+---------------------+
1 row in set (3.00 sec)
mysql> select sysdate(), sleep(3), sysdate(); +---------------------+----------+---------------------+
| sysdate() | sleep(3) | sysdate() | +---------------------+----------+---------------------+
| 2015-08-06 11:32:57 | 0 | 2015-08-06 11:33:00 | +---------------------+----------+---------------------+
1 row in set (3.00 sec)
  • 返回指定日期是星期几—————-函数:weekday(date)
mysql> select weekday('2015-08-11'); +-----------------------+
| weekday('2015-08-11') | +-----------------------+
| 1 | +-----------------------+
1 row in set (0.00 sec)
mysql> select weekday('2015-08-11 11:18:00'); +--------------------------------+
| weekday('2015-08-11 11:18:00') | +--------------------------------+
| 1 | +--------------------------------+
1 row in set (0.00 sec)
  • 返回指定日期是一个月中的第几日(在1到31范围内)——–函数:dayofmonth(date)
mysql> select dayofmonth('2015-08-11 11:18:00'); +-----------------------------------+
| dayofmonth('2015-08-11 11:18:00') | +-----------------------------------+
| 11 | +-----------------------------------+
1 row in set (0.00 sec)
  • 返回指定日期是一年中的第几日(在1到366范围内)———函数:dayofyear(date)
mysql> select dayofyear('2015-08-11 11:18:00'); +----------------------------------+
| dayofyear('2015-08-11 11:18:00') | +----------------------------------+
| 223 | +----------------------------------+
1 row in set (0.00 sec)
  • 返回指定日期中的年份—————————-函数:year(date)
mysql> select year('2015-08-11 11:18:00'); +-----------------------------+
| year('2015-08-11 11:18:00') | +-----------------------------+
| 2015 | +-----------------------------+
1 row in set (0.00 sec)
  • 返回指定日期中的月份—————————函数:month(date)
mysql> select month('2015-08-11 11:18:00'); +------------------------------+
| month('2015-08-11 11:18:00') | +------------------------------+
| 8 | +------------------------------+
1 row in set (0.00 sec)
  • 返回指定日期中的天—————————-函数:day(date)
mysql> select day('2015-08-11 11:18:00'); +----------------------------+
| day('2015-08-11 11:18:00') | +----------------------------+
| 11 | +----------------------------+
1 row in set (0.00 sec)
  • 返回指定日期是星期几(英文)——————-函数:dayname(day)
mysql> select dayname('2015-08-11 11:18:00'); +--------------------------------+
| dayname('2015-08-11 11:18:00') | +--------------------------------+
| Tuesday | +--------------------------------+
1 row in set (0.00 sec)
  • 返回指定日期的月份(英文)——————–函数:monthname(date)
mysql> select monthname('2015-08-11 11:18:00'); +----------------------------------+
| monthname('2015-08-11 11:18:00') | +----------------------------------+
| August | +----------------------------------+
1 row in set (0.00 sec)
  • 返回指定日期属于一年中的第几季度——–函数:quarter(date)
mysql> select quarter('2015-08-11 11:18:00'); +--------------------------------+
| quarter('2015-08-11 11:18:00') | +--------------------------------+
| 3 | +--------------------------------+
1 row in set (0.00 sec)
  • 返回指定日期属于一年中的第几周———函数:week(date)
mysql> select week('2015-08-11 11:18:00'); +-----------------------------+
| week('2015-08-11 11:18:00') | +-----------------------------+
| 32 | +-----------------------------+
1 row in set (0.00 sec)
  • 返回指定日期时间的HOUR—————–函数:hour(date)
mysql> select hour('2015-08-11 13:18:00'); +-----------------------------+
| hour('2015-08-11 13:18:00') | +-----------------------------+
| 13 | +-----------------------------+
1 row in set (0.00 sec)
  • 返回指定日期时间的分钟数—————函数:minute(date)
mysql> select minute('2015-08-11 13:18:00'); +-------------------------------+
| minute('2015-08-11 13:18:00') | +-------------------------------+
| 18 | +-------------------------------+
1 row in set (0.00 sec)
  • 返回指定日期时间的秒数—————-函数:second(date)
mysql> select second('2015-08-11 13:18:50'); +-------------------------------+
| second('2015-08-11 13:18:50') | +-------------------------------+
| 50 | +-------------------------------+
1 row in set (0.00 sec)

日期、时间格式化函数

  • 日期格式化函数:date_format(date,format)
mysql> select date_format('2015-08-11 13:18:50','%Y%m%d%H%i%s'); +---------------------------------------------------+
| date_format('2015-08-11 13:18:50','%Y%m%d%H%i%s') | +---------------------------------------------------+
| 20150811131850 | +---------------------------------------------------+
1 row in set (0.00 sec)
mysql> select date_format('20150811131850','%Y-%m-%d %H:%m:%s'); +---------------------------------------------------+
| date_format('20150811131850','%Y-%m-%d %H:%m:%s') | +---------------------------------------------------+
| 2015-08-11 13:08:50 | +---------------------------------------------------+
1 row in set (0.00 sec)
  • 时间格式化函数:time_format(time,format)
mysql> select time_format('131850','%H:%m:%s'); +----------------------------------+
| time_format('131850','%H:%m:%s') | +----------------------------------+
| 13:00:50 | +----------------------------------+
1 row in set (0.00 sec)
  • 字符串转换为日期函数:str_to_date(str,format)
mysql> select str_to_date('08/11/2015','%m/%d/%Y'); +--------------------------------------+
| str_to_date('08/11/2015','%m/%d/%Y') | +--------------------------------------+
| 2015-08-11 | +--------------------------------------+
1 row in set (0.00 sec)
mysql> select str_to_date('08/11/15','%m/%d/%Y'); +------------------------------------+
| str_to_date('08/11/15','%m/%d/%Y') | +------------------------------------+
| 2015-08-11 | +------------------------------------+
1 row in set (0.00 sec)
mysql> select str_to_date('08.11.15','%m.%d.%Y'); +------------------------------------+
| str_to_date('08.11.15','%m.%d.%Y') | +------------------------------------+
| 2015-08-11 | +------------------------------------+
1 row in set (0.00 sec)
mysql> select str_to_date('08.11.2015 12:40:50','%m.%d.%Y %h:%i:%s'); +--------------------------------------------------------+
| str_to_date('08.11.2015 12:40:50','%m.%d.%Y %h:%i:%s') | +--------------------------------------------------------+
| 2015-08-11 00:40:50 | +--------------------------------------------------------+
1 row in set (0.00 sec)
  • 日期、天数转换函数:to_days(date)和from_days(days)
mysql> select to_days('2015-08-11'); +-----------------------+
| to_days('2015-08-11') | +-----------------------+
| 736186 | +-----------------------+
1 row in set (0.00 sec)
  • 时间、秒转换函数:time_to_sec(time)和sec_to_time(seconds)
mysql> select time_to_sec('12:48:50'); +-------------------------+
| time_to_sec('12:48:50') | +-------------------------+
| 46130 | +-------------------------+
1 row in set (0.00 sec)
mysql> select sec_to_time('46130'); +----------------------+
| sec_to_time('46130') | +----------------------+
| 12:48:50 | +----------------------+
1 row in set (0.00 sec)
  • 拼凑日期、时间函数:makedate(year,dayofyear)和maketime(hour,minute,second)
mysql> select makedate(2015,31); +-------------------+
| makedate(2015,31) | +-------------------+
| 2015-01-31 | +-------------------+
1 row in set (0.00 sec)
mysql> select maketime(12,15,30); +--------------------+
| maketime(12,15,30) | +--------------------+
| 12:15:30 | +--------------------+
1 row in set (0.00 sec)

日期、时间计算函数

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

 - date是一个 DATETIME 或DATE值,用来指定起始时间。
 - expr是表达式,用来指定从起始日期添加或减去的时间间隔值。Expr是字符串;对于负值的时间间隔,它可以以一个 '-'开头。
 - type 为关键词,它指示了表达式被解释的方式。
 - 关键词INTERVA及type分类符均不区分大小写。
 - MySQL允许任何expr格式中的标点分隔符。若 date 参数是一个 DATE 值,而你的计算只会包括 YEAR、MONTH和DAY部分(即没有时间部分), 其结果是一个DATE值,否则结果将是一个DATETIME值。
  • 指定日期增加1秒:
mysql> select date_add('2014-12-31 23:59:59',INTERVAL 1 SECOND); +---------------------------------------------------+
| date_add('2014-12-31 23:59:59',INTERVAL 1 SECOND) | +---------------------------------------------------+
| 2015-01-01 00:00:00 | +---------------------------------------------------+
1 row in set (0.00 sec)
  • 指定日期增加1天:
mysql> select date_add('2014-12-31 23:59:59',INTERVAL 1 DAY); +------------------------------------------------+
| date_add('2014-12-31 23:59:59',INTERVAL 1 DAY) | +------------------------------------------------+
| 2015-01-01 23:59:59 | +------------------------------------------------+
1 row in set (0.00 sec)
  • 指定日期减去10小时:
mysql> select date_add('2014-12-31 00:00:00',INTERVAL '-1 10' DAY_HOUR); +-----------------------------------------------------------+
| date_add('2014-12-31 00:00:00',INTERVAL '-1 10' DAY_HOUR) | +-----------------------------------------------------------+
| 2014-12-29 14:00:00 | +-----------------------------------------------------------+
1 row in set (0.00 sec)
  • 指定日期的前一个月:
mysql> select date_sub('2014-12-31',INTERVAL 31 DAY); +----------------------------------------+
| date_sub('2014-12-31',INTERVAL 31 DAY) | +----------------------------------------+
| 2014-11-30 | +----------------------------------------+
1 row in set (0.00 sec)
  • 指定日期的前一天:
mysql> select date_sub('2014-12-31',INTERVAL -1 DAY); +----------------------------------------+
| date_sub('2014-12-31',INTERVAL -1 DAY) | +----------------------------------------+
| 2015-01-01 | +----------------------------------------+
1 row in set (0.00 sec)

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

  • 两个日期相减:
mysql> select datediff('2015-08-11','2015-07-11'); +-------------------------------------+
| datediff('2015-08-11','2015-07-11') | +-------------------------------------+
| 31 | +-------------------------------------+
1 row in set (0.00 sec)
mysql> select datediff('2015-05-11','2015-07-11'); +-------------------------------------+
| datediff('2015-05-11','2015-07-11') | +-------------------------------------+
| -61 | +-------------------------------------+
1 row in set (0.00 sec)
  • 两个时间相减:
mysql> select timediff('2015-08-11 12:12:12','2015-07-11 00:00:00'); +-------------------------------------------------------+
| timediff('2015-08-11 12:12:12','2015-07-11 00:00:00') | +-------------------------------------------------------+
| 756:12:12 | +-------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select timediff('2015-08-10 12:12:12','2015-08-11 00:00:00'); +-------------------------------------------------------+
| timediff('2015-08-10 12:12:12','2015-08-11 00:00:00') | +-------------------------------------------------------+
| -11:47:48 | +-------------------------------------------------------+
1 row in set (0.00 sec)

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

  • 日期转换为时间戳函数:timestamp(date)
mysql> select timestamp('2015-08-11'); +-------------------------+
| timestamp('2015-08-11') | +-------------------------+
| 2015-08-11 00:00:00 | +-------------------------+
1 row in set (0.00 sec)
mysql> select timestamp('2015-08-11 00:00:00','01:01:01'); +---------------------------------------------+
| timestamp('2015-08-11 00:00:00','01:01:01') | +---------------------------------------------+
| 2015-08-11 01:01:01 | +---------------------------------------------+
1 row in set (0.00 sec)
  • 时间戳增加函数:timestampadd(unit,interval,datetime_expr)
mysql> select timestampadd(day,1,'2015-08-11 12:00:00'); +-------------------------------------------+
| timestampadd(day,1,'2015-08-11 12:00:00') | +-------------------------------------------+
| 2015-08-12 12:00:00 | +-------------------------------------------+
1 row in set (0.00 sec)
  • 时间戳减少函数:timestampdiff(unit, datetime_expr1, datetime_expr2)
mysql> select timestampdiff(year,'2015-08-11','2014-08-11'); +-----------------------------------------------+
| timestampdiff(year,'2015-08-11','2014-08-11') | +-----------------------------------------------+
| -1 | +-----------------------------------------------+
1 row in set (0.00 sec)
mysql> select timestampdiff(day,'2015-08-11','2014-07-11'); +----------------------------------------------+
| timestampdiff(day,'2015-08-11','2014-07-11') | +----------------------------------------------+
| -396 | +----------------------------------------------+
1 row in set (0.00 sec)

你可能感兴趣的:(mysql,日期)