当前位置: 代码迷 >> 综合 >> mysql 日期相关函数
  详细解决方案

mysql 日期相关函数

热度:52   发布时间:2024-02-23 22:21:14.0

一、当前日期

select now();

输出:输出 2009-12-25 14:38:59

select date_format(now(), '%y-%m-%d' );

输出:09-12-25

 

根据format字符串格式化date值:

  • %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 两位数字表示的年份
  • %% 直接值“%”

set @dt = now();
--CURDATE()  当前日期
--concat(num,"%")
--TRUNCATE(X,D)
DATE_ADD() --函数向日期添加指定的时间间隔。
DATE_SUB() --函数向日期减少指定的时间间隔。
select date_add(@dt, interval 1 day); - 加1天
select date_add(@dt, interval 1 hour); -加1小时
select date_add(@dt, interval 1 minute); - 加1分钟
select date_add(@dt, interval 1 second); -加1秒
select date_add(@dt, interval 1 microsecond);-加1毫秒
select date_add(@dt, interval 1 week);-加1周
select date_add(@dt, interval 1 month);-加1月
select date_add(@dt, interval 1 quarter);-加1季
select date_add(@dt, interval 1 year);-加1年

 

  相关解决方案