MySQL DATE_FORMAT function is one of the Date Functions, which is useful to format the date and time value as per the given specifier. This section shows you how to use this MySQL DATE_FORMAT function to format the given date expression with an example.
MySQL DATE_FORMAT Syntax
The basic syntax of the MySQL DATE_FORMAT Function is as shown below:
DATE_FORMAT(Date, format_specifier);
MySQL Date Format options
The following is the list of MySQL Date format specifiers available to work or format Date and time using the Date_Format function.
Format Specifier | Description |
---|---|
%a | Abbreviated or short form of Weekday Name. For example, Sun,…, Thu…Sat |
%b | Abbreviated or short form of Month Name. For example, Jan,..Mar,…Dec. |
%c | This Date format specifier returns the Numeric Month number from 1 to 12 |
%D | Day of the Month with English Suffix. For example, 0th, 1st, 2nd, etc. |
%d | Numeric Day of the Month 00 to 31 |
%e | Day of the Month (0 to 31) |
%f | This MySQL DATE_FORMAT returns MicrosecondS FROM 000000 TO 999999 |
%H | This date format specifier will display Hour (00..23) |
%h | Hour (00..12) |
%I | Hour 01 to 12 |
%i | This specifier prints numeric Minutes values from 00 to 59 |
%j | Day of year 001 to 366 |
%k | Hour 0 to 23 |
%l | Hour (1 to 12) |
%M | This date format specifier prints Month names. For example, January,…, December. |
%m | Numeric Month number from 00 to 12 |
%p | This prints the AM or PM |
%r | The Day of the Year or Day number, i.e., 1 to 366 |
%S | Seconds 00:59 |
%s | Seconds 00:59 |
%T | This MySQL DATE_FORMAT returns Time in 24 hours format. hh:mm:ss |
%U | Equal to the WEEK() function with mode 0. Here, Sunday is the First day of the Week (00..53) |
%u | Equal to WEEK() with mode 1. Here, Monday is the First day of the Week (00..53) |
%V | Same as WEEK() mode 2. Here, Sunday is the First day of the Week (01..53). Used with %X |
%v | Equal to WEEK() mode 3. Here, Monday is the First day of the Week (01..53). Used with %x in the function |
%W | Name of the Weekday or Weekday name, i.e., Sunday, Monday,…Saturday |
%w | Weekday Number from 0 to 6 where 0 = Sunday and 6 = Saturday |
%X | Numeric Four Digit Year for the Week. Here, Sunday is the first day of the Week. Used with %V |
%x | Numeric Four Digit Year for the Week. Here, Monday is the first day of the Week. Used with %v |
%Y | Numeric Year value in Four Digits |
%y | Year in Two Digits |
%% | A literal percentage of character. |
%x | x for any not x listed above |
MySQL DATE_FORMAT Function Examples
The following are the list examples for this Date format function. The below-shown queries help you understand all the Date format specifiers available in this MYSQL Date_Format function.
MySQL DATE FORMAT Example
Here, %Y returns 4 digit Year value, and %y prints two digit year value. Next, %p returns the AM or PM based on the given MySQL time.
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%Y');
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%y');
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%p');
MySQL DATE_FORMAT Example 2
In this example, First, %a returns the short form of Day Name (Mon, Sat, etc.), and %W returns the Day Name (Sunday, Monday, etc.). Next, %U returns the Week number, where the first day is Sunday.
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%a');
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%W');
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%U');
MySQL DATE FORMAT Example 3
In this Date Function example, First, %u returns the week number, where Monday is the first day. Next, %V is used with %X, and it returns the year and Week Number where Sunday is the First day of the Week. Finally, the third statement, %x %v, returns the year and week number, where Monday is the first day.
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%u');
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%X %V');
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%x %v');
Here, %T returns the time in 24 formats of hh:mm:ss
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%x'); SELECT DATE_FORMAT('2018-12-31 23:59:02', '%X'); SELECT DATE_FORMAT('2018-12-31 23:59:02', '%T');
MySQL DATE_FORMAT Example 4
In this example, First, %b returns the short form of Month Name (Jan, Feb, Dec, etc.), and %M returns the Month Name (January,…, December). Next, %m return the month number (January = 1,…., December = 12)
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%b');
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%M');
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%m');
MySQL DATE_FORMAT Function Example 5
In this example, %H returns the hours in 24 hours format (0 to 23). Next, %h and %I return the Hours Value in 12 hours format (1 to 12).
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%H');
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%h');
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%I %p');
In this MySQL DATE_FORMAT example, %k returns the hours in 24 hours format (0 to 23), and %l returns Hours in 12 hours format (1 to 12) date.
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%k');
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%l %p');
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%i');
In this MySQL DATE_FORMAT example, First, %j format returns the Day number from date or day of the year (1 to 366). Next, %r return time in 12 hours format along with AM or PM. Finally, the third statement returns the seconds value from a given DateTime expression.
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%j');
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%r');
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%s, %S');
Here, %D returns the Day number along with the English suffix, %d, and %e returns the Day number. Next, we used the %f specifier inside this function to produce the Microseconds.
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%D');
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%d, %e');
SELECT DATE_FORMAT('2018-12-31 23:59:02.222333', '%f');
In this MySQL date_format example, we will use multiple specifiers or a combination of more than one specifier. The first statement, %W, %M %Y, returns the Weekday Name, Month Name Year. The following statement, %W, %D %M %Y returns the Weekday Name, Day Number, Month Name, and Year.
The third statement, %W, %D %M %Y %r, returns the Weekday Name, Day Number, Month Name Year, and Time in 12 hours format along with AM or PM.
SELECT DATE_FORMAT('2018-12-31 23:59:02', '%W, %M %Y'); SELECT DATE_FORMAT('2018-12-31 23:59:02', '%W, %D %M %Y'); SELECT DATE_FORMAT('2018-12-31 23:59:02', '%W, %D %M %Y %r');