MySQL DATE_FORMAT function: format the specified date

The DATE_FORMAT(date, format) function in MySQL displays the date value according to the format specified by format.

The DATE_FORMAT() function accepts two parameters:
  • date: is a valid date value to format
  • format: is a format string consisting of predefined specifiers, each preceded by a percent character (%).

The main formats are shown in the table below.

specifier illustrate
%a Abbreviated name of weekday (Sun~Sat)
%b Abbreviated name of the month (Jan…Dec)
%c month, number form (0~12)
%D Date of the month with English suffix (0th, 2st, 3nd,…)
%d The date of the month, in the form of numbers (00~31)
%e The date of the month, in the form of numbers ((0~31)
%f microseconds (000000 ... 999999)
%H Express 24 hours with 2 digits (00~23)
%h, %I Express 12 hours with 2 digits (01~12)
%i Minutes in digital form (00~59)
%j - the number of days in the year (001~366)
%k Expressed in 24 hours (0~23)
%l Expressed in 12 hours (1~12)
%M Month name (January~December)
%m Month, in digital form (00~12)
%p Morning (AM) or Afternoon (PM)
%r Time, 12-hour format (hour (hh): minute (mm): second (ss) followed by AM or PM)
%S, %s Seconds in 2-digit form (00~59)
%T Time, 24-hour format (hour(hh):minute(mm):second(ss))
%U Week (00~53), where Sunday is the first day of each week
%u Week (00~53), where Monday is the first day of the week
%V Week (01~53), where Sunday is the first day of the week, used together with %X
%v Week (01~53), where Monday is the first day of the week, used with %x
%W Day of the week (Sunday, Monday, Tuesday...Saturday)
%w —Day of the week (0=Sunday...6=Saturday)
%X The year of the week, where Sunday is the first day of the week, in numeric form, 4 digits, and %V used at the same time
%x The year of the week, where Monday is the first day of the week, in numeric form, 4 digits, used with %v
%Y 4 digits for the year
%y 2 digits for the year
%% % a literal character

[Example] Use the DATE_FORMAT() function to format and output date and time values. The input SQL statement and execution results are as follows.
mysql> SELECT DATE_FORMAT('2017-11-15 21:45:00','%W %M %D %Y') AS col1,
    -> DATE_FORMAT('2017-11-15 21:45:00','%h:%i %p %M %D %Y') AS col2;
+------------------------------+-----------------------------+
| col1                         | col2                        |
+------------------------------+-----------------------------+
| Wednesday November 15th 2017 | 09:45 PM November 15th 2017 |
+------------------------------+-----------------------------+
1 row in set (0.03 sec)
It can be seen from the running results that "2017-11-15 21:45:00" has been converted into date and time values ​​in different formats according to different parameters.

Guess you like

Origin blog.csdn.net/weixin_56175092/article/details/130365454