MySQL日期/时间函数

一、查询当前时间函数:函数

select NOW(),LOCALTIME(),SYSDATE(),CURRENT_TIMESTAMP();

可是now()与sysdate()有点差别的,一个语句中now()的值是不变的,而sysdate()是动态获取的,例如spa

select NOW(),SLEEP(2),NOW();
SELECT SYSDATE(),SLEEP(2),SYSDATE();

-------经过在MySQL中执行select sleep(N)可让此语句运行N秒钟3d

二、获取当前日期,curdate()=current_date()=current_datecode

select curdate(),current_date(),current_date;

三、获取当前时间,curtime()=current_time()=current_timeorm

select curtime(),current_time(),current_time;

四、获取年,月,日blog

select year(now()),month(now()),day(now());

五、获取当前星期几,几月,以英文返回,dayname(),monthname()字符串

select dayname(curdate()),monthname(curdate());

六、获取某个日期在周,月,年中的位置,dayofweek(),dayofmonth,dayofyear(),如要返回中文周几,能够在程序中进行逻辑ast

select dayofweek(now()),dayofmonth(now()),dayofyear(now());

七、获取一个月的最后一天,last_day(),利用它能够获得某个月有多少天form

select last_day(NOW()),day(last_day(NOW()));

八、获取某天位于一年中的第N周week(date,3)=weekofyear(),week()函数的第二个参数用来设定以星期几作为一周的开始class

select week(now(),3),weekofyear(now());

九、获取两个日期或者两个时间的相差,datediff(),timediff()

select datediff(curdate(),'2018-02-15'),timediff(curtime(),'09:09:09')

十、时间与秒的转换,time_to_sec(),sec_to_time()

select time_to_sec(@d),sec_to_time(12389);

十一、日期与天数的转换,to_days(),from_days()

十二、字符串转换为日期,str_to_date(date,format)

select str_to_date('09/09/20','%Y/%m/%d');
select str_to_date('09.09.20','%Y.%m.%d');

1三、日期/时间拼凑,makedate(year,dayofyear),maketime(hour,minute,second)

select makedate(2015,200),maketime(13,20,15);
相关文章
相关标签/搜索