select now(), current_date, current_time, current_timestamp
select date_format(now(), '%y-%m-%d %h:%i:%s');
SELECT str_to_date('2023-12-20 11:30:35','%Y-%m-%d %h:%i:%s');
# 年月日
select year(now()), month(now()), day(now());
# 时分秒
select hour(now()), minute(now()), second(now());
# 天数
select dayofmonth(now()), dayofweek(now()), dayofyear(now());
# 周数
select dayname(now()), weekday(now()), dayofweek(now());
select start, end, timediff(end, start) from date_test;
select start, end, timestampdiff(YEAR , start, end) from date_test;
select start, end, timestampdiff(MONTH , start, end) from date_test;
select start, end, timestampdiff(DAY , start, end) from date_test;
select start, end, timestampdiff(HOUR , start, end) from date_test;
select start, end, timestampdiff(MINUTE , start, end) from date_test;
select start, end, timestampdiff(SECOND , start, end) from date_test;
# 获取 7 天前的日期,注意 INTERVAL 后面表达式的区别
select date_add(now(), INTERVAL -7 day), date_sub(now(), INTERVAL 7 day);
# 获取 7 天后的日期,注意 INTERVAL 后面表达式的区别
select date_add(now(), INTERVAL 7 day), date_sub(now(), INTERVAL -7 day);
-- 本周开始
select date_sub(curdate(), INTERVAL WEEKDAY(curdate()) DAY);
-- 本周结束
select date_sub(curdate(), INTERVAL WEEKDAY(curdate()) - 6 DAY);
-- 本月开始
select date_sub(curdate(), INTERVAL DAYOFMONTH(curdate()) -1 DAY);
-- 本月结束
select last_day(curdate());