DATE_FORMAT
DATE_FORMAT is a SQL function used to format the appearance of a date value according to specified date/time format patterns.
DATE_FORMAT(date, format)
- date: The date value that will be formatted. This is the date value which needs to be converted in different formats. MySQL accepts various date formats including ‘YYYY-MM-DD’, ‘YY-MM-DD’, ‘DD-MM-YYYY’.
- format: Specifies the format that will be used to convert the given date. It is a string containing several different types of specifiers for time and date values. For instance, ‘%D’ denotes Day of the month with English suffix (0th, 1st, 2nd, 3rd), ‘%M’ denotes Month name in human-friendly letters (January, February, March, etc.).
Example
Output
Explanation
In this example, the DATE_FORMAT()
function is used along with MySQL’s NOW()
function to return the current date and time. The format string '%M %d %Y %h:%i %p'
is used to format the time into a specific format - month (in words), day, year, hours, minutes and AM/PM indicator.