MySQL 中的 DATE_FORMAT()函数
MySQL 中的 DATE_FORMAT()函数用于将指定的日期格式化为给定的格式值,即,将给出一个日期,该函数将该日期格式化为指定的格式参数。
句法 :
DATE_FORMAT(date, format)
参数:此函数接受两个参数,如下所示:
- date –要格式化的指定日期。
- 格式 -指定的格式。下面列出了此函数中使用的格式列表:
Format | Description |
---|---|
%a | This abbreviation means weekday name. It’s limit is from Sun to Sat. |
%b | This abbreviation means month name. It’s limit is from Jan to Dec. |
%c | This abbreviation means numeric month name. It’s limit is from 0 to 12. |
%D | This abbreviation means day of the month as a numeric value, followed by suffix like 1st, 2nd, etc. |
%e | This abbreviation means day of the month as a numeric value. It’s limit is from 0 to 31. |
%f | This abbreviation means microseconds. It’s limit is from 000000 to 999999. |
%H | This abbreviation means hour. It’s limit is from 00 to 23. |
%i | This abbreviation means minutes. It’s limit is from 00 to 59. |
%j | This abbreviation means day of the year. It’s limit is from 001 to 366. |
%M | This abbreviation means month name from January to December. |
%p | This abbreviation means AM or PM. |
%S | This abbreviation means seconds. It’s limit is from 00 to 59. |
%U | This abbreviation means week where Sunday is the first day of the week. It’s limit is from 00 to 53. |
%W | This abbreviation means weekday name from Sunday to Saturday. |
%Y | This abbreviation means year as a numeric value of 4-digits. |
回报:
它返回格式化的日期。
示例-1:
从指定日期“2020-11-23”获取格式化年份为“2020”。
SELECT DATE_FORMAT("2020-11-23", "%Y");
输出 :
2020
示例 2 :
从指定日期“2020-11-23”获取格式化月份名称为“十一月”。
SELECT DATE_FORMAT("2020-11-23", "%M");
输出 :
November
示例 3 :
从指定日期“2020 年 11 月 23 日”获取月份中的某一天作为数值作为“23rd”。
SELECT DATE_FORMAT("2020-11-23", "%D");
输出 :
23rd
示例 4:
从指定日期“2020-11-23”获取月日和年为“November 23 2020”。
SELECT DATE_FORMAT("2020-11-23", "%M %d %Y");
输出 :
November 23 2020
示例 5:
从指定的日期和时间“2020-11-23 12:09:23”获取小时和分钟为“12 09”。
SELECT DATE_FORMAT("2020-11-23 12:09:23", "%H %i");
输出 :
12 09
应用程序:此函数用于将指定日期格式化为给定的格式值。