MySQL DATE_FORMAT Function

MySQL DATE_FORMAT function; Through this tutorial, i am going to show you MySQL DATE_FORMAT function with the help of examples.

MySQL DATE_FORMAT() function

DATE_FORMAT() function in MySQL is used to format a specified date as given format value i.e., a date will be given and this function will format that date as specified format parameters

Syntax of MySQL DATE_FORMAT() function

The basic syntax of mysql date_format function; as follows:

DATE_FORMAT(date,format);

Parameters of MySQL DATE_FORMAT() function

NameDescription
date Where date is the date you want to format.
format format specifies how it should be formatted.

For a list of valid format specifiers, see the table below.

FormatDescription
%aAbbreviated weekday name (Sun to Sat)
%bAbbreviated month name (Jan to Dec)
%cNumeric month name (0 to 12)
%DDay of the month as a numeric value, followed by suffix (1st, 2nd, 3rd, …)
%dDay of the month as a numeric value (01 to 31)
%eDay of the month as a numeric value (0 to 31)
%fMicroseconds (000000 to 999999)
%HHour (00 to 23)
%hHour (00 to 12)
%IHour (00 to 12)
%iMinutes (00 to 59)
%jDay of the year (001 to 366)
%kHour (0 to 23)
%lHour (1 to 12)
%MMonth name in full (January to December)
%mMonth name as a numeric value (00 to 12)
%pAM or PM
%rTime in 12 hour AM or PM format (hh:mm:ss AM/PM)
%SSeconds (00 to 59)
%sSeconds (00 to 59)
%TTime in 24 hour format (hh:mm:ss)
%UWeek where Sunday is the first day of the week (00 to 53)
%uWeek where Monday is the first day of the week (00 to 53)
%VWeek where Sunday is the first day of the week (01 to 53). Used with %X
%vWeek where Monday is the first day of the week (01 to 53). Used with %X
%WWeekday name in full (Sunday to Saturday)
%wDay of the week where Sunday=0 and Saturday=6
%XYear for the week where Sunday is the first day of the week. Used with %V
%xYear for the week where Monday is the first day of the week. Used with %V
%YYear as a numeric, 4-digit value
%yYear as a numeric, 2-digit value

Example 1 – MySQL DATE_FORMAT() function

SELECT DATE_FORMAT('2015-07-20', '%W, %M %Y') AS 'Result';

Output-1

 +---------------------+
 | Result              |
 +---------------------+
 |Monday, July 2015    |
 +---------------------+

Example 2 – MySQL DATE_FORMAT() function

SELECT DATE_FORMAT('2019-05-16', '%a, %b %Y') AS 'Result';

Output-2

 +----------------+
 | Result         |
 +----------------+
 |Thu, May 2019   |
 +----------------+

Example 3 – MySQL DATE_FORMAT() function

To fetch the date with mysql query using date_format() function; as follows:

SELECT
  created_at AS 'Created_at',
  DATE_FORMAT( created_at , '%W, %M %Y') AS 'Date'
FROM customers
WHERE id= 1;

Output-3

 +---------------------+------------------------------+
 | Created_at          | Date                         |
 +---------------------+------------------------------+
 | 2005-05-25 11:30:37 | Wednesday, September 2010    |
 +---------------------+------------------------------+

Example 4 – MySQL DATE_FORMAT() function

Use DATE_FORMAT() to format the time component of a datetime value; as follows:

SELECT
  created_at AS 'Created_at',
  DATE_FORMAT(created_at, '%h:%i:%s') AS 'Time'
FROM customers
WHERE id= 1;

Output-4

+---------------------+----------+
| Created_at          | Time     |
+---------------------+----------+
| 2015-08-24 10:35:47 | 12:30:38 |
+---------------------+----------+

Let’s take a new example for add the AM/PM with time.

SELECT
  created_at AS 'Created_at',
  DATE_FORMAT(created_at, '%h:%i %p') AS 'Time'
FROM customers
WHERE id= 1;

Output

+---------------------+----------+
|  Created_at         | Time     |
+---------------------+----------+
| 2015-08-24 10:35:47 | 12:30 AM |
+---------------------+----------+
More Examples here

 Mysql> SELECT DATE_FORMAT('2019-12-31 23:59:02', '%a');
 Output>
 Tue

 Mysql> SELECT DATE_FORMAT('2019-12-31 23:59:02', '%W');
 Output>
 Tuesday

 Mysql> SELECT DATE_FORMAT('2019-12-31 23:59:02', '%U');
 Output>
 52

 Mysql> SELECT DATE_FORMAT('2019-12-31 23:59:02', '%u');
 Output>
 53

 Mysql> SELECT DATE_FORMAT('2019-12-31 23:59:02', '%X %V');
 Output>
 2019 52

Conclusion

In this MySQL tutorial, we have learned how to use mysql DATE_FORMAT() function with various examples.

Leave a Comment