一、日期/时间类型转换成字符串类型
date_format(date,format):日期转字符串
select date_format('2008-08-08 08:08:08', '%y%m%d%h%i%s');
- time_format(time,format):时间转字符串
select time_format('08:08:08', '%h%i%s');
二、字符串类型转换成日期/时间类型
- str_to_date(str, format):
select str_to_date('18/08/08' , '%y/%m/%d'); -- 2018-08-08
select str_to_date('2018/08/08', '%y/%m/%d'); -- 2018-08-08
select str_to_date('08:08:08', '%h:%i:%s'); -- 08:08:08
select str_to_date('2018.08.08 08:08:08', '%y.%m.%d %h:%i:%s'); -- 2018-08-08 08:08:08
select str_to_date('2018-08-08 08:08:08', '%y-%m-%d %h:%i:%s'); -- 2018-08-08 08:08:08
三、转换可使用的格式
| 格式 | 描述 |
|---|---|
| %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 位 |
到此这篇关于mysql日期时间类型与字符串互相转换的方法的文章就介绍到这了,更多相关mysql时间类型与字符串转换内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论