–日期 转换 时间戳
select unix_timestamp() --1640140182 bigint 当前时间戳
select unix_timestamp(‘2021-12-22 10:29:42’,‘yyyy-MM-dd hh:mm:ss’) --1640140182 bigint
select unix_timestamp(‘2021-12-22 10:29:42’);–1640140182 bigint 满足’yyyy-MM-dd hh:mm:ss’格式的,一个参数就可以
select unix_timestamp(‘2021-12-22’,‘yyyy-MM-dd’) – 1640102400 big int 不满足时间格式的,要指定格式
– 时间戳 转换 日期 返回 string 类型的时间
select from_unixtime(unix_timestamp(),‘yyyy-MM-dd hh:mm:ss’) as dt – string
select from_unixtime(1640140182,‘yyyy-MM-dd hh:mm:ss’) as date_time – string ‘2021-12-22 10:29:42’
select from_unixtime(1640140182,‘yyyy-MM-dd’) as dt – string ‘2021-12-22’
–特殊:如果unixtime为13位的,需要先转成10位
select from_unixtime(cast(1640140182488/1000 as int),‘yyyy-MM-dd HH:mm:ss’) – string ‘2021-12-22 10:29:42’
select from_unixtime(cast(substr(1640140182488,1,10) as int),‘yyyy-MM-dd HH:mm:ss’) – string ‘2021-12-22 10:29:42’
–日期字符串与日期相互转化
select to_date(‘2021-12-22 10:29:42’) – date 2021-12-22 字符串转换为日期
select cast(current_date() as string) – 直接将日期转化为 字符串 ‘2022-03-03’
select date_format(‘2021-12-22 10:29:42’,‘yyyy-MM-dd’) – string '2021-12-22’转换日期字符串格式
select current_date(); – date 2022-03-03 当前日期
select date_sub(current_date(),1); – date 2021-03-02 日期减去天数,几天前,返回的是日期
select date_add(current_date(),1); – date 2021-03-04 几天后,日期加天数,返回的是日期
select datediff(current_date(),to_date(‘2022-03-02 10:29:42’)); – int 两个日期差的天数