mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2017-06-13 14:02:16 |
+---------------------+
mysql> select sysdate();
+---------------------+
| sysdate() |
+---------------------+
| 2017-06-13 14:02:40 |
+---------------------+
1 row in set (0.00 sec)
同now()函数不同的是,now()在执行开始时得到,sysdate()在函数执行时动态得到。
mysql> select now(), sleep(3),now();
+---------------------+----------+---------------------+
| now() | sleep(3) | now() |
+---------------------+----------+---------------------+
| 2017-06-13 14:10:40 | 0 | 2017-06-13 14:10:40 |
+---------------------+----------+---------------------+
mysql> select sysdate(),sleep(3),sysdate();
+---------------------+----------+---------------------+
| sysdate() | sleep(3) | sysdate() |
+---------------------+----------+---------------------+
| 2017-06-13 14:11:02 | 0 | 2017-06-13 14:11:05 |
+---------------------+----------+---------------------+
mysql> select current_timestamp,current_timestamp();
+---------------------+---------------------+
| current_timestamp | current_timestamp() |
+---------------------+---------------------+
| 2017-06-13 14:15:52 | 2017-06-13 14:15:52 |
+---------------------+---------------------+
mysql> select DAYOFWEEK('2017-06-06');
+-------------------------+
| DAYOFWEEK('2017-06-06') |
+-------------------------+
| 3 |
+-------------------------+
mysql> select WEEKDAY('2017-06-06');
+-----------------------+
| WEEKDAY('2017-06-06') |
+-----------------------+
| 1 |
+-----------------------+
1 row in set (0.00 sec)
mysql> select DAYOFMONTH('2017-06-06');
+--------------------------+
| DAYOFMONTH('2017-06-06') |
+--------------------------+
| 6 |
+--------------------------+
mysql> select DAYOFYEAR('2017-06-06');
+-------------------------+
| DAYOFYEAR('2017-06-06') |
+-------------------------+
| 157 |
+-------------------------+
mysql> select MONTH('2017-06-06');
+---------------------+
| MONTH('2017-06-06') |
+---------------------+
| 6 |
+---------------------+
mysql> select DAYNAME('2017-06-06');
+-----------------------+
| DAYNAME('2017-06-06') |
+-----------------------+
| Tuesday |
+-----------------------+
mysql> select MONTHNAME('2017-06-06');
+-------------------------+
| MONTHNAME('2017-06-06') |
+-------------------------+
| June |
+-------------------------+
mysql> select QUARTER('2017-06-06');
+-----------------------+
| QUARTER('2017-06-06') |
+-----------------------+
| 2 |
+-----------------------+
1 row in set (0.00 sec)
mysql> select WEEK('2017-06-06',1);
+----------------------+
| WEEK('2017-06-06',1) |
+----------------------+
| 23 |
+----------------------+
mysql> select YEAR('2017-06-06');
+--------------------+
| YEAR('2017-06-06') |
+--------------------+
| 2017 |
+--------------------+
-小时数:HOUR(Time)
mysql> select HOUR('13:13:13');
+------------------+
| HOUR('13:13:13') |
+------------------+
| 13 |
+------------------+
mysql> select MINUTE('13:13:13');
+--------------------+
| MINUTE('13:13:13') |
+--------------------+
| 13 |
+--------------------+
-秒数:SECOND(Time)
mysql> select SECOND('13:13:13');
+--------------------+
| SECOND('13:13:13') |
+--------------------+
| 13 |
+--------------------+
MySQLDate/Time To Str(日期/时间转换成字符串)函数:date_format(date,format),time_format(time,format) date参数是合法的日期,format规定日期/时间的输出格式* 可以使用的格式有
格式 | 描述 |
---|---|
%a | 缩写星期名 |
%b | 缩写月名 |
%c | 月,数值 |
%D | 带有英文前缀的月中的天 |
%d | 月的天,数值(00-31) |
%e | 月的天,数值(0-31) |
%f | 微秒 |
%H | 小时 (00-23) |
%h | 小时 (01-12) |
%I | 小时 (01-12) |
%i | 分钟,数值(00-59) |
%j | 年的天 (001-366) |
%k | 小时 (0-23) |
%l | 小时 (1-12) |
%M | 月名 |
%m | 月,数值(00-12) |
%p | AM 或 PM |
%r | 时间,12-小时(hh:mm:ss AM 或 PM) |
%S | 秒(00-59) |
%s | 秒(00-59) |
%T | 时间, 24-小时 (hh:mm:ss) |
%U | 周 (00-53) 星期日是一周的第一天 |
%u | 周 (00-53) 星期一是一周的第一天 |
%V | 周 (01-53) 星期日是一周的第一天,与 %X 使用 |
%v | 周 (01-53) 星期一是一周的第一天,与 %x 使用 |
%W | 星期名 |
%w | 周的天 (0=星期日, 6=星期六) |
%X | 年,其中的星期日是周的第一天,4 位,与 %V 使用 |
%x | 年,其中的星期一是周的第一天,4 位,与 %v 使用 |
%Y | 年,4 位 |
%y | 年,2 位 |
str_to_date(str,format)函数作为date_format(date,format)的逆函数,能够吧字符串转换为时间 str参数为待转义的字符串,format是转换格式,格式含义同date_format一样。
select str_to_date('06/06/2017', '%m/%d/%Y'); -- 2017-06-06
select str_to_date('06/06/17' , '%m/%d/%y'); -- 2017-06-06
select str_to_date('06.06.2017', '%m.%d.%Y'); -- 2017-06-06
select str_to_date('08:08:30', '%h:%i:%s'); -- 08:08:30
select str_to_date('06.06.2017 06:06:30', '%m.%d.%Y %h:%i:%s'); -- 2017-06-06 06:06:30
select to_days('2017-06-06'); -- 736851 select from_days(736851); -- '2017-06-06'
time_to_sec('06:06:06'); -- 21966 select sec_to_time(21966); -- 06:06:06
select makedate(2017,160); -- 2017-06-06
select maketime(13,13,13); -- 13:13:13
Type取值 |
---|
MICROSECOND |
SECOND |
MINUTE |
HOUR |
DAY |
WEEK |
MONTH |
QUARTER |
YEAR |
SECOND_MICROSECOND |
MINUTE_MICROSECOND |
MINUTE_SECOND |
HOUR_MICROSECOND |
HOUR_SECOND |
HOUR_MINUTE |
DAY_MICROSECOND |
DAY_SECOND |
DAY_MINUTE |
DAY_HOUR |
YEAR_MONTH |
datediff('2017/05/02', '2016/11/15'); -- 168
timediff('11:12:13','10:03:14'); -- 01:08:59
imestamp('2017-06-06'); -- 2017-06-06 00:00:00
select timestamp('2017-06-06 08:08:08', '12:12:12'); -- 2017-06-06 20:20:20
timestampdiff(hour,'2017-06-06 12:00:00','2017-06-07 13:13:00'); -- 25
select timestampdiff(day,'2017-06-06 12:00:00','2017-06-07 13:13:00'); -- 1