MySQL numeric functions
MySQL string functions
MySQL Date/Time functions
MySQL aggregate functions
MySQL flow control functions
The DATE_FORMAT()
function in MySQL is used to format a date value based on a specified format. It allows you to choose the format in which the date should be returned, making it very versatile for displaying date values in various formats.
The syntax for DATE_FORMAT()
is:
DATE_FORMAT(date, format)
Where date
is the date value you want to format, and format
is the format in which you want the date to be returned.
Here are some of the commonly used format specifiers:
%Y
: Four-digit year%y
: Two-digit year%m
: Month, (01..12)%d
: Day of the month, (00..31)%H
: Hour (00..23)%i
: Minutes, (00..59)%s
: Seconds (00..59)%p
: AM or PMHere's an example of using DATE_FORMAT()
:
SELECT DATE_FORMAT(NOW(), '%M %d, %Y');
This would return the current date in a format like 'May 11, 2023'.
And another example:
SELECT DATE_FORMAT('2023-05-11 14:23:00', '%H:%i %p, %M %d %Y');
This would return '14:23 PM, May 11 2023'.
You can combine these specifiers in any way you like to create the date format that best fits your needs.
Remember that DATE_FORMAT()
returns a string, so the result can't be used as a date in date functions, but is useful for display purposes or when a string result is required.
MySQL DATE_FORMAT Function Example:
DATE_FORMAT
function is used to format date and time values according to a specified format.-- Example formatting a date in MySQL SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s') AS formatted_date;
Using DATE_FORMAT to Display Month Names in MySQL:
%M
to display the full month name and %b
for the abbreviated month name.-- Example displaying the full month name SELECT DATE_FORMAT(NOW(), '%M') AS month_name;
Formatting Day, Month, and Year Separately in MySQL:
%d
for day, %m
for month, and %Y
for the year to format each component separately.-- Example formatting day, month, and year separately SELECT DATE_FORMAT(NOW(), '%d') AS day, DATE_FORMAT(NOW(), '%m') AS month, DATE_FORMAT(NOW(), '%Y') AS year;
Converting Timestamps to Readable Dates with DATE_FORMAT:
FROM_UNIXTIME
to convert timestamps to datetime values and then apply DATE_FORMAT
for formatting.-- Example converting timestamp to readable date SELECT DATE_FORMAT(FROM_UNIXTIME(1609459200), '%Y-%m-%d %H:%i:%s') AS readable_date;
MySQL DATE_FORMAT for Displaying Weekdays:
%W
for the full weekday name and %a
for the abbreviated weekday name.-- Example displaying the full weekday name SELECT DATE_FORMAT(NOW(), '%W') AS weekday_name;
Formatting Hours and Minutes with DATE_FORMAT in MySQL:
%H
for 24-hour format, %h
for 12-hour format, %i
for minutes, and %p
for AM/PM.-- Example formatting hours and minutes SELECT DATE_FORMAT(NOW(), '%H:%i') AS formatted_time_24h, DATE_FORMAT(NOW(), '%h:%i %p') AS formatted_time_12h;
Changing the Order of Day, Month, and Year with DATE_FORMAT:
-- Example changing the order of day, month, and year SELECT DATE_FORMAT(NOW(), '%m-%d-%Y') AS formatted_date;
MySQL DATE_FORMAT for Formatting Date Ranges:
DATE_FORMAT
to both the start and end dates to format a date range.-- Example formatting date range SELECT CONCAT(DATE_FORMAT(start_date, '%Y-%m-%d'), ' to ', DATE_FORMAT(end_date, '%Y-%m-%d')) AS date_range FROM date_range_table;
Formatting Dates with Leading Zeros in MySQL:
%d
with %m
to ensure day and month have leading zeros when needed.-- Example formatting dates with leading zeros SELECT DATE_FORMAT(NOW(), '%d/%m/%Y') AS formatted_date;