本文为个人笔记,有错误请见谅!
返回当前日期时间
select from_unixtime(unix_timestamp()); --2020-07-28 16:34:58
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss'); --2020-07-28 16:32:30
select current_timestamp; --2020-07-28 16:34:21.448返回当前日期
select current_date; --2020-07-28
select from_unixtime(unix_timestamp(),'yyyy-MM-dd'); --2020-07-28返回当前时间戳
select unix_timestamp(); --1595925165-----------------------------------------------------------------------------------------时间戳转日期
select from_unixtime(1595865600,'yyyy-MM-dd'); --2020-07-28
select from_unixtime(1595865600,'yyyyMMdd'); --20200728
select from_unixtime(1595865600); --2020-07-28 00:00:00
select from_unixtime(unix_timestamp('20200728','yyyyMMdd'),'yyyy-MM-dd'); --2020-07-28日期转时间戳
select unix_timestamp('2020-07-28','yyyy-MM-dd'); --1595865600
select unix_timestamp('20200728','yyyyMMdd'); --1595865600
select unix_timestamp('2020-07-28 15:17:20', 'yyyy-MM-dd HH:mm:ss'); --1595920640日期格式转换
2020/07/28 15:25:10 转成正常格式(yyyy-MM-dd hh:mm:ss)
select from_unixtime(to_unix_timestamp('2020/07/28 15:25:10','yyyy/MM/dd HH:mm:ss')); --2020-07-28 15:25:10
20200728152510 转成(yyyy/MM/dd hh:mm:ss)
select from_unixtime(to_unix_timestamp('20200728152510','yyyyMMddhhmmss'),'yyyy/MM/dd HH:mm:ss'); --2020/07/28 15:25:10-----------------------------------------------------------------------------------------返回日期时间中的日期部分
select to_date('2020-07-28 16:13:57'); --2020-07-28返回日期中的年
select year('2020-07-28 16:13:57'); --2020
返回日期中的月
select month('2020-07-28 16:13:57'); --7
返回日期中的日
select day('2020-07-28 16:13:57'); --28
返回日期中的时
select hour('2020-07-28 16:13:57'); --16
返回日期中的分
select minute('2020-07-28 16:13:57'); --13
返回日期中的秒
select second('2020-07-28 16:13:57'); --57-----------------------------------------------------------------------------------------将日期时间进行格式化
select date_format('2020-07-28','yyyy-MM'); --2020-07
select date_format('2020-07-28 16:48:20','yyyy-MM'); --2020-07
select date_format('2020-07-28','yyyyMMdd'); --20200728返回结束日期减去开始日期的差值
select datediff('2020-07-28','2020-07-21'); --7日期的加减
select date_add('2020-07-28',10); --2020-08-07
select date_sub('2020-07-28',10); --2020-07-18取当前天的下一个周一(说明:星期一到星期日的英文:Monday、Tuesday、Wednesday、Thursday、Friday、Saturday、Sunday,英语单词不区分大小写)
select next_day('2020-07-28','MO'); --2020-08-03
取当前周的周一
select date_add(next_day('2020-07-28','MO'),-7); --2020-07-27返回当月的第一天
select trunc('2020-07-28','MM'); --2020-07-01返回当年的第一天
select trunc('2020-07-28','YEAR'); --2020-01-01求当月最后一天日期
select last_day('2020-07-28'); --2020-07-31返回日期在当前的周数
select weekofyear('2020-07-28 16:13:57'); --31