时间格式转换整理
Mysql里的时间格式转换有DATE_FORMAT()、STR_TO_DATE(),本文主要列出详细时间格式。
延展阅读链接: 60个Mysql日期时间函数汇总
- Step 1:了解当前数据的区域编码
为了更好的了解时间格式,我们先从国际化讲起,这里涉及到Locale(地区编码),众所周知实际上有许多国家,有不同的语言,那么对时间的显示自然不同。比如我们想表达日期每周的第一天,那么在美国是Sunday,在中国则是星期一。这里涉及到Mysql里的一个系统参数
lc_time_names
SELECT @@lc_time_names locate_name;
-- locate_name
-- en_US
SELECT DAYNAME('2019-05-12')WeekNo,MONTHNAME('2019-05-12')Month_no;
-- WeekNo Month_no
-- Sunday May
--设置区域是中国
SET lc_time_names = 'zh_CN';
SELECT DAYNAME('2019-05-12')WeekNo,MONTHNAME('2019-05-12')Month_no;
-- WeekNo Month_no
-- 星期日 五月
更多的区域编码有:ja_JP、en_AU、zh_HK、zh_TW etc。这里不做过多的展开。
- Step 2:实践时间转换的格式
常见的时间格式见下图:
实际案例见下:
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y') dateformat;
-- dateformat
-- Sunday October 2009
SELECT DATE_FORMAT('2007-10-04 22:23:00', '%H:%i:%s')dateformat;
-- dateformat
-- 22:23:00
SELECT DATE_FORMAT('1900-10-04 22:23:00', '%D %y %a %d %m %b %j')dateformat;
-- dateformat
-- 4th 00 Thu 04 10 Oct 277
SELECT DATE_FORMAT('1997-10-04 22:23:00', '%H %k %I %r %T %S %w')dateformat;
-- dateformat
-- 22 22 10 10:23:00 PM 22:23:00 00 6
SELECT DATE_FORMAT('1999-01-01', '%X %V')dateformat;
-- dateformat
-- 1998 52
SELECT DATE_FORMAT('2006-06-00', '%d')dateformat;
-- dateformat
-- 00
-- 指定区域编码
SELECT DATE_FORMAT('2019-05-12', '%W', 'zh_CN')dateformat;
-- dateformat
-- 星期日
SELECT STR_TO_DATE('2006-06-06', '%Y-%m-%d')dateformat;
-- dateformat
-- 2006/6/6 0:00:00
-- %W结合%#
SELECT str_to_date('Wednesday23423, June 2, 2014', '%W%#, %M %e, %Y')dateformat;
-- dateformat
-- 2014/6/2 0:00:00
-- 我们可以通过约定的时间格式转换时间,如:
SELECT STR_TO_DATE('2003-12-31',GET_FORMAT(DATE,'ISO')) dateformat;
-- dateformat
-- 2003/12/31 0:00:00
- Step3:附录:
时间格式列表
序号 | 格式 | 英文含义 | 中文含义 |
1 | %a | Abbreviated weekday name (Sun..Sat) | 周序号的缩写,显示结果依赖区域编码 |
2 | %b | Abbreviated month name (Jan..Dec) | 月序号的缩写,显示结果依赖区域编码 |
3 | %c | Month, numeric (0..12) | 数字月,1到12里的某个值 |
4 | %D | Day of the month with English suffix (0th, 1st, 2nd, 3rd, …) | 英文系月份里日序号,如0th、12th |
5 | %d | Day of the month, numeric (00..31) | 月份里日序号,均按照两个数字显示 |
6 | %e | Day of the month, numeric (0..31) | 月份里日序号,按照正常数字显示。可能是一位或者两位数字。 |
7 | %f | Microseconds (000000..999999) | 微秒,这里一般适用时间精确到微秒的时间。 |
8 | %H | Hour (00..23) | 小时,均按照两为数显示,这里是按照24小时制计算 |
9 | %h | Hour (01..12) | 小时,均按照两为数显示,这里是按照12小时制计算 |
10 | %I | Hour (01..12) | 小时,均按照两为数显示,这里是按照12小时制计算 |
11 | %i | Minutes, numeric (00..59) | 分钟,均按照两为数显示 |
12 | %j | Day of year (001..366) | 一年中所在天的序号,均按照3位数显示 |
13 | %k | Hour (0..23) | 小时,均按照一位数显示,这里是按照24小时制计算 |
14 | %l | Hour (1..12) | 小时,均按照一位数显示,这里是按照12小时制计算 |
15 | %M | Month name (January..December) | 月份,显示结果依赖区域编码 |
16 | %m | Month, numeric (00..12) | 数字形式的月份 |
17 | %p | AM or PM | 上下午,am是上午,pm是下午,过了12点就算pm |
18 | %r | Time, 12-hour (hh:mm:ss followed by AM or PM) | 时:分:秒 am/pm形式的时间 |
19 | %S | Seconds (00..59) | 两位的秒数 |
20 | %s | Seconds (00..59) | 两位的秒数 |
21 | %T | Time, 24-hour (hh:mm:ss) | 24制时:分:秒 |
22 | %U | Week (00..53), where Sunday is the first day of the week | 一年所在周序号,均两位数显示,这里周日的序号是1,序号从00开始 |
23 | %u | Week (00..53), where Monday is the first day of the week | 一年所在周序号,均两位数显示,这里周一的序号是1,序号从00开始 |
24 | %V | Week (01..53), where Sunday is the first day of the week; used with %X | 一年所在周序号,均两位数显示,这里周日的序号是1,序号从01开始 |
25 | %v | Week (01..53), where Monday is the first day of the week; used with %x | 一年所在周序号,均两位数显示,这里周一的序号是1,序号从01开始 |
26 | %W | Weekday name (Sunday..Saturday) | 周详细信息,如区域编码是zh_CN时显示为:星期一 |
27 | %w | Day of the week (0=Sunday..6=Saturday) | 周序号,这里周日是0,周一是1 |
28 | %X | Year with 4 digits when first day of the week is Sunday. Used with %V. | 四位年加所在周,该参数结合%V一起使用,周日为一周第一天 |
29 | %x | Year with 4 digits when first day of the week is Monday. Used with %v. | 四位年加所在周,该参数结合%v一起使用,周一为一周第一天 |
30 | %Y | Year, numeric, four digits | 四位年 |
31 | %y | Year, numeric (two digits) | 后两位年 |
32 | %# | For str_to_date(), skip all numbers. | 过滤时间字符串格式里所有的数字 |
33 | %. | For str_to_date(), skip all punctation characters. | 过滤时间字符串格式里所有的标点符号 |
34 | %@ | For str_to_date(), skip all alpha characters. | 过滤时间字符串格式里所有的字母 |
35 | %% | A literal % character. | 处理时间字符串格式里转义字符 |
约定的时间格式列表
约定时间格式函数 | 格式结果 |
GET_FORMAT(DATE,'EUR') | '%d.%m.%Y' |
GET_FORMAT(DATE,'USA') | '%m.%d.%Y' |
GET_FORMAT(DATE,'JIS') | '%Y-%m-%d' |
GET_FORMAT(DATE,'ISO') | '%Y-%m-%d' |
GET_FORMAT(DATE,'INTERNAL') | '%Y%m%d' |
GET_FORMAT(DATETIME,'EUR') | '%Y-%m-%d %H.%i.%s' |
GET_FORMAT(DATETIME,'USA') | '%Y-%m-%d %H.%i.%s' |
GET_FORMAT(DATETIME,'JIS') | '%Y-%m-%d %H:%i:%s' |
GET_FORMAT(DATETIME,'ISO') | '%Y-%m-%d %H:%i:%s' |
GET_FORMAT(DATETIME,'INTERNAL') | '%Y%m%d%H%i%s' |
GET_FORMAT(TIME,'EUR') | '%H.%i.%s' |
GET_FORMAT(TIME,'USA') | '%h:%i:%s %p' |
GET_FORMAT(TIME,'JIS') | '%H:%i:%s' |
GET_FORMAT(TIME,'ISO') | '%H:%i:%s' |
GET_FORMAT(TIME,'INTERNAL') | '%H%i%s' |