mysql中的日期函数

– 日期函数:

– 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();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值