SELECT DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s') as date_str;
+---------------------+
| date_str |
+---------------------+
| 2019-06-10 11:36:10 |
+---------------------+
SELECT TIME_FORMAT(NOW(),'%H:%i:%s') as time_str;
+----------+
| time_str |
+----------+
| 11:36:31 |
+----------+
字符串转时间:
SELECT str_to_date('2019-06-10 11:36:10','%Y-%m-%d %H:%i:%s') as dt;
+---------------------+
| dt |
+---------------------+
| 2019-06-10 11:36:10 |
+---------------------+
## 如果只希望获取时间
SELECT str_to_date('11:36:10','%H:%i:%s') as dt;
+----------+
| dt |
+----------+
| 11:36:10 |
+----------+
不同国家使用不同的时间格式,可以使用get_format函数获取默认时间格式。
get_format函数用法:
get_format(date|time|datetime, 'eur'|'usa'|'jis'|'iso'|'internal' );
如获取USA的默认时间格式
select
get_format(date,'usa') as usa_date_format,
get_format(time,'usa') as usa_time_format,
get_format(datetime,'usa') as usa_datetime_format;
+-----------------+-----------------+---------------------+
| usa_date_format | usa_time_format | usa_datetime_format |
+-----------------+-----------------+---------------------+
| %m.%d.%Y | %h:%i:%s %p | %Y-%m-%d %H.%i.%s |
+-----------------+-----------------+---------------------+