DATE_FORMAT
DATE_FORMAT(date, format)
Section titled “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
Section titled “Example”SELECT DATE_FORMAT(NOW(), '%M %d %Y %h:%i %p');Output
Section titled “Output”June 15 2022 06:00 PMExplanation
Section titled “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.