MySQL日期时间处理函数大全

MySQL 日期时间处理函数大全

-- 1、查看当前日期时间
select now() from dual; -- 这里面的dual为虚表,MYSQL也可以不写。
select now();
select current_timestamp();
select current_timestamp;
select localtime();
select localtime;
select localtimestamp();
select localtimestamp;
-- 初次之外还有sysdate,但是特殊些。
select sysdate();
-- 区别:
select now(), sleep(3), now();
select sysdate(), sleep(3), sysdate();
 
 -- 查看当前日期
select curdate();
select current_date();
select current_date;
-- 产看当前时间
select curtime();
select current_time();
select current_time;
 
-- 获得当前 utc 日期时间函数
select utc_timestamp(), utc_date(), utc_time()
-- mysql 获得当前时间戳函数:
select current_timestamp, current_timestamp();
 

-- 2、日期函数

select date(current_timestamp);-- 获取日期
select time(current_timestamp);-- 获取时间
select year(current_timestamp);-- 获取年份
select month(current_timestamp);-- 获取月份
select day(current_timestamp);-- 获取日
select hour(current_timestamp);-- 获取时
select minute(current_timestamp);-- 获取分
select second(current_timestamp);-- 获取秒
select microsecond();-- 获取毫秒
select quarter(current_timestamp);-- 获取季度
select week(current_timestamp);-- 获取周
select weekofyear(current_timestamp);-- 同week()
select dayofyear(current_timestamp);-- 日期在年度中第几天
select dayofmonth(current_timestamp);-- 日期在月度中第几天
select dayofweek(current_timestamp);-- 日期在周中第几天;周日为第一天
select weekday(current_timestamp);-- 日期在本周的星期几 0-6,0为星期1
select yearweek(current_timestamp);-- 年和周

-- 3、extract函数用法
select extract(year from current_timestamp); -- 年
select extract(month from current_timestamp);-- 月
select extract(day from current_timestamp);-- 日
select extract(hour from current_timestamp);-- 小时
select extract(minute from current_timestamp);-- 分钟
select extract(second from current_timestamp);-- 秒
select extract(microsecond from '2022-05-20 8:24:30.666666');-- 毫秒
select extract(quarter from current_timestamp);-- 季度
select extract(week from current_timestamp);-- 第几周
select extract(year_month from current_timestamp); -- 年和月
select extract(day_hour from '2022-05-20 8:24:30.666666'); -- 日时
select extract(day_minute from '2022-05-20 8:24:30.666666');-- 日时分)
select extract(day_second from '2022-05-20 8:24:30.666666');-- 日时分秒
select extract(day_microsecond from '2022-05-20 8:24:30.666666');-- 日时分秒毫秒
select extract(hour_minute from current_timestamp);-- 时分
select extract(hour_second from current_timestamp);-- 时分秒
select extract(hour_microsecond from '2022-05-20 8:24:30.666666');-- 日时分秒毫秒
select extract(minute_second from current_timestamp);-- 分秒
select extract(minute_microsecond from '2022-05-20 8:24:30.666666');-- 分秒毫秒
select extract(second_microsecond from '2022-05-20 8:24:30.666666');-- 秒毫秒
select dayname(current_timestamp);-- monday(返回英文星期)
select monthname(current_timestamp);-- may(返回英文月份)
select last_day(current_timestamp);-- 返回月份中最后一天

 
-- 4、date_add(date,interval expr type) 从日期加上指定的时间间隔
select date_add(current_timestamp,interval 1 year);
select date_add(current_timestamp,interval 1 quarter);
select date_add(current_timestamp,interval 1 month);
select date_add(current_timestamp,interval 1 week);
select date_add(current_timestamp,interval 1 day);
select date_add(current_timestamp,interval 1 hour);
select date_add(current_timestamp,interval 1 minute);
select date_add(current_timestamp,interval 1 second);
select date_add('2022-05-20 8:24:30.666666',interval 1 microsecond);
 
 
-- 5、date_sub(date,interval expr type) 从日期减去指定的时间间隔
select date_sub(current_timestamp,interval 1 year);
select date_sub(current_timestamp,interval 1 quarter);
select date_sub(current_timestamp,interval 1 month);
select date_sub(current_timestamp,interval 1 week);
select date_sub(current_timestamp,interval 1 day);
select date_sub(current_timestamp,interval 1 hour);
select date_sub(current_timestamp,interval 1 minute);
select date_sub(current_timestamp,interval 1 second);
select date_sub('2022-05-20 8:24:30.666666',interval 1 microsecond);
 
-- 6、 经特殊日期测试,date_sub(date,interval expr type)可放心使用
select date_sub(curdate(),interval 1 day);-- 前一天
select date_sub(curdate(),interval -1 day);-- 后一天
select date_sub(curdate(),interval 1 month);-- 一个月前日期
select date_sub(curdate(),interval -1 month);-- 一个月后日期
select date_sub(curdate(),interval 1 year);-- 一年前日期
select date_sub(curdate(),interval -1 year);-- 一年后日期
-- mysql date_sub() 日期时间函数 和 date_add() 用法一致,并且可以用internal -1 xxx的形式互换使用;
-- 另外,mysql 中还有两个函数 subdate(), subtime(),建议,用 date_sub() 来替代。
 
 
-- 7、mysql 另类日期函数:period_add(p,n), period_diff(p1,p2)
-- 函数参数“p” 的格式为“yyyymm” 或者 “yymm”,第二个参数“n” 表示增加或减去 n month(月)。
-- mysql period_add(p,n):日期加/减去n月。
select period_add(202205,2), period_add(202205,-2);  -- 加两个月减两个月
select period_diff(20220608, 20220508);  -- 差12月,第一个参数减第二个参数
select datediff('2022-06-05','2022-05-29'); -- 差7天
select timediff('2022-06-05 19:28:37', '2022-06-05 17:00:00');
 
-- 8、mysql日期转换函数
select time_to_sec('08:24:30');
select sec_to_time(30270);
-- mysql (日期、天数)转换函数:to_days(date), from_days(days)
select to_days('0000-00-00'); -- null 
select to_days(CURRENT_TIMESTAMP); -- 738652
select from_days(0);
select from_days(738652);

 
-- 9、mysql str to date (字符串转换为日期)函数:str_to_date(str, format)
select str_to_date('06.05.2017 19:40:30', '%m.%d.%y %h:%i:%s');
select str_to_date('06/05/2017', '%m/%d/%y'); 
select str_to_date('2017/12/3','%y/%m/%d')
select str_to_date('20:09:30', '%h:%i:%s')		       
 
-- 10、日期时间格式化
select date_format('2022-05-12 17:03:51', '%y年%m月%d日 %h时%i分%s秒');
select time_format('2022-05-12 17:03:51', '%y年%m月%d日 %h时%i分%s秒');
-- str_to_date()和date_formate()为互逆操作
  
-- mysql 拼凑日期、时间函数:makdedate(year,dayofyear), maketime(hour,minute,second)
select makedate(2012,30);
select maketime(19,52,35);

-- 11、mysql (unix 时间戳、日期)转换函数
-- unix_timestamp(), unix_timestamp(date), from_unixtime(unix_timestamp), from_unixtime(unix_timestamp,format)
select unix_timestamp();
select unix_timestamp('2022-05-12');
select unix_timestamp('2022-05-12 08:24:30');
-- 将时间戳转为具体时间
select from_unixtime(1652315070); -- 时间戳转化成日期
select from_unixtime(1652315070, '%y年%m月%d日 %h时%分:%s秒'); -- 时间戳转化成格式化日期
 
-- 12、mysql 时间戳(timestamp)转换、增、减函数
select timestamp('2022-05-12');
select timestamp('2022-05-12 08:12:25', '01:01:01'); -- 时间加
select date_add('2022-05-12 08:12:25', interval 1 day); -- 下一天
select timestampadd(day, 1, '2022-05-12 08:12:25');-- 下一天
-- mysql timestampadd() 函数类似于 date_add()。
 
select timestampdiff(year, '2017-06-01', CURRENT_DATE); -- 差几年
select timestampdiff(month, '2017-06-01', CURRENT_DATE);  -- 差多少月
select timestampdiff(day, '2017-06-01', CURRENT_DATE); -- 差多少天
select timestampdiff(hour, '2017-06-01 08:12:25', CURRENT_TIMESTAMP);-- 差多少小时
select timestampdiff(minute, '2017-06-01 08:12:25',CURRENT_DATE);-- 差多少分钟
select timestampdiff(second, '2017-06-01 08:12:25', CURRENT_DATE);-- 差多少秒

本文部分资料参考:
[https://blog.csdn.net/qinshijangshan/article/details/72874667?spm=1001.2014.3001.5506]

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值