w3resource

MySQL DATE_FORMAT() function

DATE_FORMAT() function

MySQL DATE_FORMAT() formats a date as specified in the argument. A list of format specifiers given bellow may be used to format a date. The ‘%’ is required before the format specifier characters. It's a versatile function that helps present date and time information in various desired formats.

This function is useful in -

  • The primary purpose of the DATE_FORMAT() function is to allow you to customize the way date and datetime values are displayed.
  • In graphical user interfaces (GUIs), DATE_FORMAT() helps display dates in formats that match the UI design, creating a cohesive user experience.
  • DATE_FORMAT() is essential for filtering and sorting date-based data in specific formats, ensuring accurate comparisons and ordering.
  • In applications that are used in different countries, DATE_FORMAT() helps adapt date representations to the local date format conventions.
  • When exporting or importing data to/from external systems, DATE_FORMAT() ensures that the date format is consistent with the target system's requirements.
  • DATE_FORMAT() is used to format dates in a way that is familiar to users based on their cultural or regional preferences.

Syntax:

DATE_FORMAT(date,format)

Arguments:

Name Description
date A date.
format Indicates how to format a date.

Syntax Diagram:

MySQL DATE_FORMAT() Function - Syntax Diagram

MySQL Version: 8.0

Table of format specifiers

Name Description
%a Abbreviated weekday name (Sun..Sat)
%b Abbreviated month name (Jan..Dec)
%c Month, numeric (0..12)
%D Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%d Day of the month, numeric (00..31)
%e Day of the month, numeric (0..31)
%f Microseconds (000000..999999)
%H Hour (00..23)
%h Hour (01..12)
%I Hour (01..12)
%i Minutes, numeric (00..59)
%j Day of year (001..366)
%k Hour (0..23)
%l Hour (1..12)
%M Month name (January..December)
%m Month, numeric (00..12)
%p AM or PM
%r Time, 12-hour (hh:mm:ss followed by AM or PM)
%S Seconds (00..59)
%s Seconds (00..59)
%T Time, 24-hour (hh:mm:ss)
%U Week (00..53), where Sunday is the first day of the 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 with %X
%v Week (01..53), where Monday is the first day of the week; used with %x
%W Weekday name (Sunday..Saturday)
%w Day of the week (0=Sunday..6=Saturday)
%X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%Y Year, numeric, four digits
%y Year, numeric (two digits)
%% A literal “%” character
%x x, for any “x” not listed above

Pictorial Presentation:

Pictorial Presentation of MySQL DATE_FORMAT() function

Example: MySQL DATE_FORMAT() function

select date_format(date, '%a %D %b %Y') 
as formatted_date 
from table_name;

Where date is the name of your date field, and formatted_date is a column alias which you can use as a column heading.

Example Date: 11th February 2011
Replace date with the name of your date field...
date_format String Example
'%Y-%m-%d' 2011-02-11
'%e/%c/%Y' 11/2/2011 UK
'%c/%e/%Y' 2/11/2011 US
'%d/%m/%Y' 11/02/2011 UK
'%m/%d/%Y' 02/11/2011 US
'%e/%c/%Y %H:%i' 11/2/2011 12:30 UK
'%c/%e/%Y %H:%i' 2/11/2011 12:30 US
'%d/%m/%Y %H:%i' 11/02/2011 12:30 UK
'%m/%d/%Y %H:%i' 02/11/2011 12:30 US
'%e/%c/%Y %T' 11/2/2011 12:30:10 UK
'%c/%e/%Y %T' 2/11/2011 12:30:10 US
'%d/%m/%Y %T' 11/02/2011 12:30:10 UK
'%m/%d/%Y %T' 02/11/2011 12:30:10 US
'%a %D %b %Y' Fri 11th Feb 2011
'%a %D %b %Y %H:%i' Fri 11th Feb 2011 12:30
'%a %D %b %Y %T' Fri 11th Feb 2011 12:30:10
'%a %b %e %Y' Fri Feb 11 2011
'%a %b %e %Y %H:%i' Fri Feb 11 2011 12:30
'%a %b %e %Y %T' Fri Feb 11 2011 12:30:10
'%W %D %M %Y' Friday 11th February 2011
'%W %D %M %Y %H:%i' Friday 11th February 2011 12:30
'%W %D %M %Y %T' Friday 11th February 2011 12:30:10
'%l:%i %p %b %e, %Y' 12:30 PM Feb 11, 2011
'%M %e, %Y' February 11, 2011

The following statement will format the specified datetime 2008-05-15 22:23:00 according to the format specifier %W %D %M %Y. Here date has been formatted with week day name, day of the month with english suffix, month name and year in numeric.

Code:

SELECT DATE_FORMAT('2008-05-15 22:23:00', '%W %D %M %Y');

Output:

mysql> SELECT DATE_FORMAT('2008-05-15 22:23:00', '%W %D %M %Y');
+---------------------------------------------------+
| DATE_FORMAT('2008-05-15 22:23:00', '%W %D %M %Y') |
+---------------------------------------------------+
| Thursday 15th May 2008                            | 
+---------------------------------------------------+
1 row in set (0.01 sec)

Example: DATE_FORMAT() function with (%r) specifier

The following statement will format the specified datetime 2008-05-15 22:23:00 according to the format specifier %r. Here the function returns the time in 12-hour format followed by AM or PM.

Code:

SELECT DATE_FORMAT('2008-05-15 22:23:00', '%r');

Output:

mysql> SELECT DATE_FORMAT('2008-05-15 22:23:00', '%r');
+------------------------------------------+
| DATE_FORMAT('2008-05-15 22:23:00', '%r') |
+------------------------------------------+
| 10:23:00 PM                              | 
+------------------------------------------+
1 row in set (0.00 sec)

Example: DATE_FORMAT() function using table

The following statement will format the specified ‘ord_date’ column from purchase table according to the format specifier %W %D %M %Y and display all the rows.

Sample table: purchase


Code:

SELECT invoice_no,ord_date, DATE_FORMAT(ord_date,'%W %D %M %Y')
FROM purchase; 

Output:

 mysql> SELECT invoice_no,ord_date,DATE_FORMAT(ord_date,'%W %D %M %Y')
    ->       FROM purchase;
+------------+------------+-------------------------------------+
| invoice_no | ord_date   | DATE_FORMAT(ord_date,'%W %D %M %Y') |
+------------+------------+-------------------------------------+
| INV0001    | 2008-07-06 | Sunday 6th July 2008                | 
| INV0002    | 2008-08-09 | Saturday 9th August 2008            | 
| INV0003    | 2008-09-15 | Monday 15th September 2008          | 
| INV0004    | 2007-08-22 | Wednesday 22nd August 2007          | 
| INV0005    | 2007-06-25 | Monday 25th June 2007               | 
| INV0006    | 2007-09-20 | Thursday 20th September 2007        | 
+------------+------------+-------------------------------------+
6 rows in set (0.09 sec)
 

Example: DATE_FORMAT() function with where

The following statement will format the specified ‘ord_date’ column from purchase table according to the format specifier %W %D %M %Y and returns those orders which had been placed after 2007.

Sample table: purchase


Code:

SELECT invoice_no,ord_date,            
DATE_FORMAT(ord_date,'%W %D %M %Y')
FROM purchase
WHERE DATE_FORMAT(ord_date,' %Y')>2007;

Output:

 mysql> SELECT invoice_no,ord_date, DATE_FORMAT(ord_date,'%W %D %M %Y')
    ->       FROM purchase
    ->       WHERE DATE_FORMAT(ord_date,' %Y')>2007;
+------------+------------+-------------------------------------+
| invoice_no | ord_date   | DATE_FORMAT(ord_date,'%W %D %M %Y') |
+------------+------------+-------------------------------------+
| INV0001    | 2008-07-06 | Sunday 6th July 2008                | 
| INV0002    | 2008-08-09 | Saturday 9th August 2008            | 
| INV0003    | 2008-09-15 | Monday 15th September 2008          | 
+------------+------------+-------------------------------------+
3 rows in set (0.01 sec)

Video Presentation:

All Date and Time Functions :

Click here to see the MySQL Date and time functions.

Previous: DATE_ADD()
Next: DATE_SUB()



Follow us on Facebook and Twitter for latest update.