select current_date() as today;
select current_timestamp() as today;
select unix_timestamp() as today;
select from_unixtime(1653363939) as today;
注:第二个参数为时间格式,默认是’yyyy-MM-dd HH:mm:ss’
select from_unixtime(unix_timestamp(),'yyyy-MM-dd') as today;
注:to_date() 默认转化格式为’yyyy-MM-dd’
select to_date('2022-05-23 23:00:01') as today;
select date_format('2022-05-23 23:00:01','yyyy-MM-dd') as today;
select date_format('2022-05-23','yyyy-MM') as month;
select date_format('2022-05-23','yyyy') as year;
select year('2022-05-01') as year;
select quarter('2022-05-23') as quarter;
select month('2022-05-01') as month;
select day('2022-05-23') as day;
select hour('2022-05-23 02:00:01') as hour;
select minute('2022-05-23 23:00:01') as minute;
select second('2022-05-23 23:00:01') as second;
select date_add('2022-05-23',1) as tomorrow;
select date_add('2022-05-23',-7) as lastweek;
select date_sub('2022-05-23',1) as yeasterday;
select date_sub('2022-05-23',-1) as nextday;
select add_months('2022-05-23',1) as nextmonth;
select add_months('2022-05-23',-1) as lastmonth;
select weekofyear('2022-01-01') as 01week;
select weekofyear('2022-01-08') as 08week;
select datediff('2022-01-08 00:00:01', '2022-02-08 00:02:00') as diff;
select datediff('2022-03-08', '2022-02-08') as difftwo;
select last_day('2022-02-05') as day;
注:next_day()第⼆个参数⽀持⼩写、⼤写、缩写(su/sun/sunday)
select next_day('2022-05-24','su') as nextsunday;
select next_day('2022-05-24','MON') as nextmonday;
select trunc('2022-05-22','YY') as year;
select trunc('2022-05-22','MM') as month;
//2022-05-23是周一
//本周第一天
select date_sub(next_day('2022-05-23','monday'),7) as monday;
//上周第一天
select date_sub(next_day('2022-05-25','monday'),14) as lastmonday;
//本月第一天
select trunc('2022-05-31', 'MM') as month;
//本月最后一天
select last_day('2022-02-05') as day;
//上月第一天
select trunc(add_months('2022-05-31',-1), 'MM') as lastmonth;