– 日期函数:
– 1.获取时间戳,从1970-01-01 00:00:00到当前的毫秒值
select unix_timestamp();
– 2.将一个日期字符串转为毫秒值
select unix_timestamp('2021-12-21 08:08:08');
– 3.将时间戳毫秒值转为指定的格式:
select from_unixtime(1640045288,'%Y-%m-%d %H:%i:%s');
– 4.获取当前的年月日
select curdate();
select current_date();
– 5.获取当前的时分秒
select current_time();
select curtime();
– 6.获取年月日和时分秒
select current_timestamp();
– 7.从日期字符串中获取年月日
select date('2002-12-12 12:34:56');
– 8.获取日期之间的差值
select datediff('2021-12-23','2008-08-08');
select datediff(current_date(),'2008-08-08');
– 9.获取时间的差值
select timediff('12:12:34','10:18:56');
– 10.日期格式化
select date_format('2021-1-1 1:1:1','%Y-%m-%d %H:%i:%s')
– 11.将字符串转为日期
select str_to_date('2021-1-1 1:1:1','%Y-%m-%d %H:%i:%s')
– 12将日期进行加减法 --日期向前跳转
select date_sub('2021-10-01' interval 2 day);
select date_sub('2021-10-01' interval 2 month);
– 13将日期进行加法 – 日期向后跳转
select date_add('2021-10-01' interval 2 day);
select date_add('2021-10-01' interval 2 day);
– 14.从日期中获取小时
select extract(hour from '2021-12-13 11:12:13');
select extract(year from '2021-12-13 11:12:13');
select extract(month from '2021-12-13 11:12:13');
– 15.获取给定日期的最后一天,所在月的最后一天
select last_day('2021-08-13');
– 16.获取指定年份和天数的日期
– 2021年的第53天
select makedate('2021',53);
– 17根据日期获取年月日时分秒
select year ('2021-12-13 11:12:13');
select month ('2021-12-13 11:12:13');
select minute ('2021-12-13 11:12:13');
select quarter ('2021-12-13 11:12:13'); -- 获取季度
– 18.根据日期获取信息
select MONTHNAME('2021-12-13 11:12:13'); -- 获取月份英文
select DAYNAME('2021-12-13 11:12:13'); -- 获取周几:Monda。1表示周日,2为周一
select DAYOFMONTH('2021-12-13 11:12:13'); -- 当月的第几天
select DAYOFWEEK('2021-12-13 11:12:13'); -- 1表示周日,2为周一
select DAYOFYEAR('2021-12-13 11:12:13'); -- 获取一年的第几天
select week('2021-12-13 11:12:13'); -- 一年的第几周
select YEARWEEK('2021-3-01');
select NOW();