MySQL: DATE_FORMAT function; In this tutorial, we will discuss how to use MySQL DATE_FORMAT function with the help of examples.
The MySQL DATE_FORMAT() function allows you to return a date in a specified format. For example, you can use it to return 2019-05-15 as May 15 2019 , or whatever other format you require.
MySQL DATE_FORMAT() function
The DATE_FORMAT() function formats a date as specified.
Syntax:
DATE_FORMAT(date,format);
Parameters or Arguments
Name | Description |
---|---|
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.
Format | Description |
---|---|
%a | Abbreviated weekday name (Sun to Sat) |
%b | Abbreviated month name (Jan to Dec) |
%c | Numeric month name (0 to 12) |
%D | Day of the month as a numeric value, followed by suffix (1st, 2nd, 3rd, …) |
%d | Day of the month as a numeric value (01 to 31) |
%e | Day of the month as a numeric value (0 to 31) |
%f | Microseconds (000000 to 999999) |
%H | Hour (00 to 23) |
%h | Hour (00 to 12) |
%I | Hour (00 to 12) |
%i | Minutes (00 to 59) |
%j | Day of the year (001 to 366) |
%k | Hour (0 to 23) |
%l | Hour (1 to 12) |
%M | Month name in full (January to December) |
%m | Month name as a numeric value (00 to 12) |
%p | AM or PM |
%r | Time in 12 hour AM or PM format (hh:mm:ss AM/PM) |
%S | Seconds (00 to 59) |
%s | Seconds (00 to 59) |
%T | Time in 24 hour format (hh:mm:ss) |
%U | Week where Sunday is the first day of the week (00 to 53) |
%u | Week where Monday is the first day of the week (00 to 53) |
%V | Week where Sunday is the first day of the week (01 to 53). Used with %X |
%v | Week where Monday is the first day of the week (01 to 53). Used with %X |
%W | Weekday name in full (Sunday to Saturday) |
%w | Day of the week where Sunday=0 and Saturday=6 |
%X | Year for the week where Sunday is the first day of the week. Used with %V |
%x | Year for the week where Monday is the first day of the week. Used with %V |
%Y | Year as a numeric, 4-digit value |
%y | Year as a numeric, 2-digit value |
Here are several examples to display MySQL FORMAT_DATE ().
Example-1
SELECT DATE_FORMAT('2015-07-20', '%W, %M %Y') AS 'Result';
Output-1
+---------------------+ | Result | +---------------------+ |Monday, July 2015 | +---------------------+
Example-2
Here’s an example that formats the result into short day and month names.
SELECT DATE_FORMAT('2019-05-16', '%a, %b %Y') AS 'Result';
Output-2
+----------------+ | Result | +----------------+ |Thu, May 2019 | +----------------+
Must Know Date/Time Functions
List Of MySQL Date/Time FunctionsExample-3 Of Database
Here, we will take example of database table users, where we format the value returned from a database column that’s stored as a datetime value.
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 => Formatting the Time
You could use DATE_FORMAT()
to format the time component of a datetime value.
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 discussed mysql DATE_FORMAT() function with various examples.
Recommended MySQL Tutorials
If you have any questions or thoughts to share, use the comment form below to reach us.