mysql time_format() 函数介绍与用法
墨初 Web前端 1793阅读
mysql语句中的 time_format 函数与mysql语句中的date_format() 函数的用法相似,但不同的是 date_format() 函数是对日期进行格式化而time_format函数是对时间进行格式化。
mysql time_format() 函数介绍
time_format():对指定的时间进行定义的格式进行输出。
语法:
time_format(time,format)
参数:
| 参数 | 描述 |
|---|---|
| time | 必需,需要格式化时间。 |
| format | 必需,格式化后的时间格式,需要使用限定符,限定符参考下面的列表。 |
时间预定义格式符列表:
| 限定符 | 含义 |
|---|---|
| %H | 24小时格式的小时,前导加0,例如:00,01..23 |
| %h | 小时,12小时格式,带前导零,例如:01,02 … 12 |
| %I | 与 %h 相同 |
| %i | 分数为零,例如:00,01,… 59 |
| %k | 24小时格式的小时,无前导零,例如:0,1,2 … 23 |
| %l | 12小时格式的小时,无前导零,例如:0,1,2 … 12 |
| %p | AM或PM,取决于其他时间说明符 |
| %r | 表示时间,12小时格式hh:mm:ss AM或PM |
| %S | 表示秒,前导零,如:00,01,… 59 |
| %s | 与 %S 相同 |
| %T | 表示时间,24小时格式hh:mm:ss |
mysql time_format() 使用示例
例1:
time_format() 输出带有 am pm 后缀的时间格式
mysql> select time_format('11:21:22','%r');
+------------------------------+
| time_format('11:21:22','%r') |
+------------------------------+
| 11:21:22 AM |
+------------------------------+
1 row in set (0.00 sec)
mysql> select time_format('15:21:22','%r');
+------------------------------+
| time_format('15:21:22','%r') |
+------------------------------+
| 03:21:22 PM |
+------------------------------+
1 row in set (0.00 sec)例2:
time_format() 输出 hh:ii:ss 格式的时间
mysql> select time_format('01:21:22','%T');
+------------------------------+
| time_format('01:21:22','%T') |
+------------------------------+
| 01:21:22 |
+------------------------------+
1 row in set (0.00 sec)
mysql> select time_format('1:21:22','%T');
+-----------------------------+
| time_format('1:21:22','%T') |
+-----------------------------+
| 01:21:22 |
+-----------------------------+
1 row in set (0.00 sec)例3:
time_format() 输出 12小时与24小时格式的时间
mysql> select time_format('21:21:22','%h:%i:%s');
+------------------------------------+
| time_format('21:21:22','%h:%i:%s') |
+------------------------------------+
| 09:21:22 |
+------------------------------------+
1 row in set (0.00 sec)
mysql> select time_format('21:21:22','%H:%i:%s');
+------------------------------------+
| time_format('21:21:22','%H:%i:%s') |
+------------------------------------+
| 21:21:22 |
+------------------------------------+
1 row in set (0.00 sec) 标签:mysql