1、date() 函数:
select date('2014-11-09 16:25:00')
#2014-11-09
2、
select curDate(); #获取当前日期
#2018-03-02
select curtime(); #获取当前时间
#10:19:51
select now(); #获取当前时间+日期
#2018-03-02 10:20:51
3、date_add()
列举1个天数(年、月、日.......)加减的例子:
select date_add(now(), interval 1 day); #当前日期天数+1
select date_add(now(), interval -1 day); #当前日期天数-1
select date_add(now(), interval 1 hour);
select date_add(now(), interval 1 minute);
select date_add(now(), interval 1 second);
select date_add(now(), interval 1 microsecond);
select date_add(now(), interval 1 week);
select date_add(now(), interval 1 month);
select date_add(now(), interval 1 quarter);
select date_add(now(), interval 1 year);
4、date_sub() 与date_add功能正好相反
select date_sub(now(), interval 1 day); #当前日期天数-1
select date_sub(now(), interval -1 day);#当前日期天数+1
select date_sub(now(), interval 1 hour);
select date_sub(now(), interval 1 minute)
select date_sub(now(), interval 1 second)
select date_sub(now(), interval 1 microsecond)
select date_sub(now(), interval 1 week)
select date_sub(now(), interval 1 month)
select date_sub(now(), interval 1 quarter)
select date_sub(now(), interval 1 year);
5、datediff() 计算两个日期间隔的天数:
datediff(date1, date2); #date1 - date2
select datediff(now(), '2018-02-01');
select datediff(now(), '2018-02-01'), datediff('2018-02-02', '2018-02-01');
6、timediff() 计算两个时间间隔:
timediff(time1, time2); #time1 - time2
#time1和time2的格式必须要一样,返回时间差
select timediff('16:10:01', '15:00:00'); #01:10:01
7、str_to_date(str, format) 字符串转换为日期:
select str_to_date('11/09/2014', '%m/%d/%Y');
#2014-11-09
8、选取日期时间的各个部分:
转载自: http://blog.csdn.net/l1028386804/article/details/51155467
- set @dt = '2008-09-10 07:15:30.123456';
- select date(@dt); -- 2008-09-10
- select time(@dt); -- 07:15:30.123456
- select year(@dt); -- 2008
- select quarter(@dt); -- 3
- select month(@dt); -- 9
- select week(@dt); -- 36
- select day(@dt); -- 10
- select hour(@dt); -- 7
- select minute(@dt); -- 15
- select second(@dt); -- 30
- select microsecond(@dt); -- 123456
8、Extract() 函数,可以上面实现类似的功能:
- set @dt = '2008-09-10 07:15:30.123456';
- select extract(year from @dt); -- 2008
- select extract(quarter from @dt); -- 3
- select extract(month from @dt); -- 9
- select extract(week from @dt); -- 36
- select extract(day from @dt); -- 10
- select extract(hour from @dt); -- 7
- select extract(minute from @dt); -- 15
- select extract(second from @dt); -- 30
- select extract(microsecond from @dt); -- 123456
- select extract(year_month from @dt); -- 200809
- select extract(day_hour from @dt); -- 1007
- select extract(day_minute from @dt); -- 100715
- select extract(day_second from @dt); -- 10071530
- select extract(day_microsecond from @dt); -- 10071530123456
- select extract(hour_minute from @dt); -- 715
- select extract(hour_second from @dt); -- 71530
- select extract(hour_microsecond from @dt); -- 71530123456
- select extract(minute_second from @dt); -- 1530
- select extract(minute_microsecond from @dt); -- 1530123456
- select extract(second_microsecond from @dt); -- 30123456