获取当前时间
#获取时间
select curdate() ,current_date(),curtime(),now(),sysdate()
,utc_date(),utc_time()
from dual;
日期与时间戳的转换
#获取当前时间毫秒数 res: 1659777754
select UNIX_TIMESTAMP()
FROM dual;
#将指定毫秒数转化为时间 FROM_UNIXTIME(1659777754) res:2022-08-06 09:22:34
select FROM_UNIXTIME(1659777754)
from dual;
#将指定时间数转化为毫秒 UNIX_TIMESTAMP res:1659777754
select UNIX_TIMESTAMP('2022-08-06 09:22:34')
from dual;
获取月份 星期 日
# res: 2022
select YEAR('2022-08-06 09:22:34');
# res: 8
select MONTH('2022-08-06');
# res: 34
select second('09:22:34');
# res: 31
select week('2022-08-06');
# res: 7 (周六)
select dayofweek('2022-08-06');
日期函数
extract()
# res: 一年中的第几周
select weekofyear('2022-08-06');
# res: 一年中的第几周
select extract(week from now())
# res: 一月中的第几天
select extract(day from now()) ;
时间和秒钟的转化
#一天中的第几秒,从早上八点开始
select time_to_sec(now());
#从早上过了多久 09:50:58
select sec_to_time(35458);
计算时间和日期的函数
其他日期类函数
# INTERVAL:时间间隔 正数 加多久 ,负数 减少多久 (年 日 月)
# date_add 增加时间
# date_sub 减少时间
select date_add(now(),INTERVAL -1 year )
from dual;
#INTERVAL '1_1' year_month 加一年再加一个月
select date_add(now(),INTERVAL '1_1' year_month )
from dual;
#返回两个日期时间间隔 时:分:秒 res:838:59:59
select timediff(now(),'2022-08-06 09:22:34');
#增加指定时间(时分秒) res: 2022-08-06 10:56:19 2022-08-06 09:55:16
select now(),subtime(now(),'1:1:3') ;
日期解析
格式化年月日
#res 2022-08-06
select date_format(now(),'%Y-%m-%d')