mysql时间操作

获取当前时间

select now();

2022-01-15 15:31:34.0

获取当前时间的前1天时间

select DATE_SUB(CURRENT_TIMESTAMP,INTERVAL 1 day);

可选值如下:

字符串转date

select str_to_date(DATE_SUB(now(),INTERVAL 0 day),'%Y-%m-%d %H:%i:%s');

时间格式化

select date_format(now(),'%Y-%m-%d %H:%i:%s');


pgsql时间操作

获取当前时间

select now();

2022-01-15 15:44:19.619571

获取当前时间的前1天时间

select now() - interval '1 day'

SELECT to_char(date_trunc('hour', now()), 'YYYY-MM-DD HH24:MI:SS') AS formatted_time;

可选值如下:

时间格式化

select to_timestamp(字符串,'yyyy-mm-dd hh24:mi:ss') - interval '1 day'

时间转日期

select  now()::date 

字符串转时间

to_timestamp('2022-01-22 00:00:00','yyyy-MM-dd hh24:mi:ss')

获取年月日时分秒

select extract(year from now()) || extract(month from now()),extract(day from now()),extract(hour from now()),extract(minute from now()),extract(second from now())

字符串截取

select substr(now(),1,19)

获取前半个小时数据

select case when extract(minute from now())>=30 then substr(to_timestamp(now(),'yyyy-mm-dd hh24')+ interval '0 MINUTE',1,19) else substr(to_timestamp(now(),'yyyy-mm-dd hh24')- interval '30 MINUTE',1,19) end 
union all
select case when extract(minute from now())>=30 then substr(to_timestamp(now(),'yyyy-mm-dd hh24')+ interval '30 MINUTE',1,19) else substr(to_timestamp(now(),'yyyy-mm-dd hh24'),1,19) end 

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐