一、获取当前日期和时间
1. 获取当前日期和时间
-- 当前完整日期时间 select now(); -- 2026-02-28 14:30:45 select current_timestamp; -- 2026-02-28 14:30:45 select localtime(); -- 2026-02-28 14:30:45 select localtimestamp; -- 2026-02-28 14:30:45 select sysdate(); -- 2026-02-28 14:30:45 -- 细微区别 select now(), sleep(2), now(); -- 两个now()值相同 select sysdate(), sleep(2), sysdate(); -- sysdate()值不同
2. 获取当前日期(无时间)
select curdate(); -- 2026-02-28 select current_date(); -- 2026-02-28 select current_date; -- 2026-02-28 select date(now()); -- 2026-02-28
3. 获取当前时间(无日期)
select curtime(); -- 14:30:45 select current_time(); -- 14:30:45 select current_time; -- 14:30:45 select time(now()); -- 14:30:45
二、日期提取函数
1. 提取日期各部分
-- 提取年份
select year('2026-02-28'); -- 2026
select year(curdate()); -- 2026
select yearweek('2026-02-28'); -- 202609
-- 提取季度
select quarter('2026-02-28'); -- 1(第1季度)
-- 提取月份
select month('2026-02-28'); -- 2
select monthname('2026-02-28'); -- february
-- 提取周
select week('2026-02-28'); -- 9(一年中的第几周)
select weekofyear('2026-02-28'); -- 9
select weekday('2026-02-28'); -- 5(0=周一,5=周六)
select dayofweek('2026-02-28'); -- 7(1=周日,7=周六)
-- 提取日
select day('2026-02-28'); -- 28
select dayofmonth('2026-02-28'); -- 28
select dayofyear('2026-02-28'); -- 59(一年中的第几天)
-- 提取小时、分钟、秒
select hour('2026-02-28 14:30:45'); -- 14
select minute('2026-02-28 14:30:45'); -- 30
select second('2026-02-28 14:30:45'); -- 45
-- 提取微秒
select microsecond('2026-02-28 14:30:45.123456'); -- 1234562. 日期部分组合提取
-- 提取日期时间
select date('2026-02-28 14:30:45'); -- 2026-02-28
select time('2026-02-28 14:30:45'); -- 14:30:45
-- 提取unix时间戳
select unix_timestamp('2026-02-28 14:30:45'); -- 1772303445
select unix_timestamp(); -- 当前时间戳三、日期格式化和解析
1. 日期格式化(date_format)
-- 基本格式化
select date_format(now(), '%y-%m-%d %h:%i:%s'); -- 2026-02-28 14:30:45
select date_format(now(), '%y/%m/%d'); -- 2026/02/28
select date_format(now(), '%d-%b-%y'); -- 28-feb-2026
-- 详细格式选项
select
date_format(now(), '%y'), -- 2026(四位年份)
date_format(now(), '%y'), -- 26(两位年份)
date_format(now(), '%m'), -- february(月份全称)
date_format(now(), '%b'), -- feb(月份简称)
date_format(now(), '%m'), -- 02(两位月份)
date_format(now(), '%c'), -- 2(月份,不带前导零)
date_format(now(), '%d'), -- 28(两位日期)
date_format(now(), '%e'), -- 28(日期,不带前导零)
date_format(now(), '%h'), -- 14(24小时制,两位)
date_format(now(), '%h'), -- 02(12小时制,两位)
date_format(now(), '%i'), -- 02(12小时制,两位)
date_format(now(), '%k'), -- 14(24小时制,不带前导零)
date_format(now(), '%l'), -- 2(12小时制,不带前导零)
date_format(now(), '%i'), -- 30(两位分钟)
date_format(now(), '%s'), -- 45(两位秒)
date_format(now(), '%p'), -- pm(am/pm)
date_format(now(), '%w'), -- saturday(星期全称)
date_format(now(), '%a'), -- sat(星期简称)
date_format(now(), '%w'), -- 6(星期,0=周日,6=周六)
date_format(now(), '%j'), -- 059(一年中的第几天)
date_format(now(), '%u'), -- 09(周,周日开始)
date_format(now(), '%u'), -- 09(周,周一开始)
date_format(now(), '%t'), -- 14:30:45(24小时时间)
date_format(now(), '%r'); -- 02:30:45 pm(12小时时间)2. 日期解析(str_to_date)
-- 字符串转日期
select str_to_date('28/02/2026', '%d/%m/%y'); -- 2026-02-28
select str_to_date('2026-feb-28', '%y-%b-%d'); -- 2026-02-28
select str_to_date('02/28/2026 14:30', '%m/%d/%y %h:%i'); -- 2026-02-28 14:30:00
select str_to_date('saturday, february 28, 2026', '%w, %m %d, %y'); -- 2026-02-283. 时间格式化(time_format)
select time_format('14:30:45', '%h:%i:%s'); -- 14:30:45
select time_format('14:30:45', '%h:%i %p'); -- 02:30 pm四、日期计算和运算
1. 日期加减(date_add / date_sub)
-- 日期加法
select date_add('2026-02-28', interval 1 day); -- 2026-03-01
select date_add('2026-02-28 14:30:45', interval 1 hour); -- 2026-02-28 15:30:45
select date_add('2026-02-28', interval 1 month); -- 2026-03-28
select date_add('2026-02-28', interval 1 year); -- 2027-02-28
select date_add('2026-02-28', interval '1:30' hour_minute); -- 2026-02-28 15:30:00
-- 日期减法
select date_sub('2026-02-28', interval 1 day); -- 2026-02-27
select date_sub('2026-02-28 14:30:45', interval 30 minute); -- 2026-02-28 14:00:45
-- 简化写法
select '2026-02-28' + interval 1 day; -- 2026-03-01
select '2026-02-28' - interval 1 week; -- 2026-02-212. 时间间隔类型
-- 支持的时间间隔单位 select now() + interval 1 second; -- 加1秒 select now() + interval 1 minute; -- 加1分钟 select now() + interval 1 hour; -- 加1小时 select now() + interval 1 day; -- 加1天 select now() + interval 1 week; -- 加1周 select now() + interval 1 month; -- 加1月 select now() + interval 1 quarter; -- 加1季度 select now() + interval 1 year; -- 加1年 -- 组合间隔 select now() + interval '1 2:30:45' day_second; -- 加1天2小时30分45秒 select now() + interval '1-2' year_month; -- 加1年2个月 select now() + interval '10:30' minute_second; -- 加10分30秒
3. 日期差计算
-- 日期差(天数)
select datediff('2026-02-28', '2026-02-20'); -- 8(天数差)
select datediff('2026-02-20', '2026-02-28'); -- -8
-- 时间差(时间部分)
select timediff('14:30:45', '10:15:20'); -- 04:15:25
select timediff('2026-02-28 14:30:45', '2026-02-28 10:15:20'); -- 04:15:25
-- 时间戳差(秒数)
select timestampdiff(second, '2026-02-28 10:00:00', '2026-02-28 14:30:45'); -- 16245
select timestampdiff(minute, '2026-02-28 10:00:00', '2026-02-28 14:30:45'); -- 270
select timestampdiff(hour, '2026-02-28 10:00:00', '2026-02-28 14:30:45'); -- 4
select timestampdiff(day, '2026-02-20', '2026-02-28'); -- 8
select timestampdiff(month, '2026-01-15', '2026-03-15'); -- 2
select timestampdiff(year, '2024-02-28', '2026-02-28'); -- 2五、日期生成和构造
1. 生成日期
-- 构造日期
select makedate(2026, 59); -- 2026-02-28(2026年第59天)
select maketime(14, 30, 45); -- 14:30:45
-- 从部分构造日期
select str_to_date('2026,59', '%y,%j'); -- 2026-02-28(年份 + 一年中的第几天)2. 获取特定日期
-- 获取月份最后一天
select last_day('2026-02-15'); -- 2026-02-28
select last_day('2026-01-15'); -- 2026-01-31
select last_day('2026-02-28'); -- 2026-02-28
-- 获取月份第一天
select date_format('2026-02-28', '%y-%m-01'); -- 2026-02-01
select date_sub('2026-02-28', interval dayofmonth('2026-02-28')-1 day); -- 2026-02-01
-- 获取周的开始/结束
select date_add('2026-02-28', interval -weekday('2026-02-28') day); -- 本周一
select date_add('2026-02-28', interval 6-weekday('2026-02-28') day); -- 本周日六、日期验证和调整
1. 日期验证
-- 检查日期有效性
select isdate('2026-02-28'); -- 1(有效)
select isdate('2026-13-45'); -- 0(无效)
select isdate('2026-02-29'); -- 0(2026年不是闰年)
-- 闰年检查(间接方式)
select day(last_day('2026-02-01')); -- 28(2026年2月有28天)
select day(last_day('2024-02-01')); -- 29(2024年2月有29天)2. 日期调整
-- 调整到指定日期部分
select date('2026-02-28 14:30:45'); -- 2026-02-28
select time('2026-02-28 14:30:45'); -- 14:30:45
-- 提取并调整
select extract(year from '2026-02-28'); -- 2026
select extract(year_month from '2026-02-28'); -- 202602
select extract(day_hour from '2026-02-28 14:30:45'); -- 2814七、日期和时间戳转换
1. unix时间戳转换
-- 时间转时间戳
select unix_timestamp('2026-02-28 14:30:45'); -- 1772303445
select unix_timestamp(); -- 当前时间戳
-- 时间戳转时间
select from_unixtime(1772303445); -- 2026-02-28 14:30:45
select from_unixtime(1772303445, '%y-%m-%d %h:%i:%s'); -- 2026-02-28 14:30:45
-- 时间戳格式化
select date_format(from_unixtime(1772303445), '%y-%m-%d'); -- 2026-02-282. 其他时间单位转换
-- 秒数转时间
select sec_to_time(3661); -- 01:01:01
select time_to_sec('01:01:01'); -- 3661
-- 天数转换
select to_days('2026-02-28'); -- 738885(从公元0年开始的天数)
select from_days(738885); -- 2026-02-28八、实用查询示例
1. 查询最近n天的数据
-- 最近7天 select * from orders where order_date >= curdate() - interval 7 day; -- 最近30天 select * from logs where log_time >= date_sub(now(), interval 30 day); -- 本月数据 select * from sales where year(sale_date) = year(curdate()) and month(sale_date) = month(curdate());
2. 统计查询
-- 按天统计 select date(create_time) as date, count(*) as count from users where create_time >= curdate() - interval 30 day group by date(create_time) order by date desc; -- 按小时统计 select hour(create_time) as hour, count(*) as count from logs where date(create_time) = curdate() group by hour(create_time) order by hour;
3. 日期范围查询
-- 今天的数据 select * from activities where date(activity_time) = curdate(); -- 昨天的数据 select * from activities where date(activity_time) = curdate() - interval 1 day; -- 本周数据(周一开始) select * from activities where yearweek(activity_time) = yearweek(curdate()); -- 本月数据 select * from activities where year(activity_time) = year(curdate()) and month(activity_time) = month(curdate());
4. 时间计算
-- 计算年龄
select
birth_date,
timestampdiff(year, birth_date, curdate()) as age
from users;
-- 计算工作时长(分钟)
select
start_time,
end_time,
timestampdiff(minute, start_time, end_time) as duration_minutes
from work_sessions;
-- 计算剩余天数
select
datediff(deadline, curdate()) as days_remaining
from projects;九、性能优化建议
-- ❌ 不推荐:使用函数导致索引失效 select * from orders where date(order_time) = '2026-02-28'; -- ✅ 推荐:使用范围查询可以利用索引 select * from orders where order_time >= '2026-02-28 00:00:00' and order_time < '2026-02-29 00:00:00'; -- ❌ 不推荐:在where子句中使用函数 select * from users where year(create_time) = 2026 and month(create_time) = 2; -- ✅ 推荐:使用范围查询 select * from users where create_time >= '2026-02-01' and create_time < '2026-03-01';
十、mysql 8.0 新增函数
-- mysql 8.0+ 新增函数
select date('2026-02-28 14:30:45'); -- 已存在
select time('2026-02-28 14:30:45'); -- 已存在
-- 时区转换(8.0+)
select convert_tz('2026-02-28 14:30:45', '+00:00', '+08:00');
-- 提取时间部分(8.0+)
select extract(year from '2026-02-28'); -- 2026
select extract(month from '2026-02-28'); -- 2
select extract(day from '2026-02-28'); -- 28
-- 获取系统时区(8.0+)
select @@system_time_zone;总结表格
函数类别 | 常用函数 | 用途说明 |
|---|---|---|
获取当前 | now(), curdate(), curtime() | 获取当前日期时间 |
日期提取 | year(), month(), day() | 提取日期各部分 |
日期计算 | date_add(), date_sub(), datediff() | 日期加减和计算 |
日期格式化 | date_format(), str_to_date() | 日期格式转换 |
特殊日期 | last_day(), makedate() | 生成特定日期 |
时间戳 | unix_timestamp(), from_unixtime() | unix时间戳转换 |
时间差 | timestampdiff(), timediff() | 计算时间差 |
使用建议:
- 尽量使用原生日期类型存储日期时间
- 在where子句中避免对字段使用函数,以免索引失效
- 根据业务需求选择合适的日期函数
- 注意时区问题,统一使用utc或业务时区
- 考虑mysql版本兼容性
到此这篇关于mysql 日期和时间函数示例详解的文章就介绍到这了,更多相关mysql日期和时间函数内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论