MySQL DATE_FORMAT Function: Format The Specified Date

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 PM

Here'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.

  1. MySQL DATE_FORMAT Function Example:

    • Description: The DATE_FORMAT function is used to format date and time values according to a specified format.
    • Code:
      -- Example formatting a date in MySQL
      SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s') AS formatted_date;
      
  2. Using DATE_FORMAT to Display Month Names in MySQL:

    • Description: Use %M to display the full month name and %b for the abbreviated month name.
    • Code:
      -- Example displaying the full month name
      SELECT DATE_FORMAT(NOW(), '%M') AS month_name;
      
  3. Formatting Day, Month, and Year Separately in MySQL:

    • Description: Use %d for day, %m for month, and %Y for the year to format each component separately.
    • Code:
      -- 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;
      
  4. Converting Timestamps to Readable Dates with DATE_FORMAT:

    • Description: Use FROM_UNIXTIME to convert timestamps to datetime values and then apply DATE_FORMAT for formatting.
    • Code:
      -- Example converting timestamp to readable date
      SELECT DATE_FORMAT(FROM_UNIXTIME(1609459200), '%Y-%m-%d %H:%i:%s') AS readable_date;
      
  5. MySQL DATE_FORMAT for Displaying Weekdays:

    • Description: Use %W for the full weekday name and %a for the abbreviated weekday name.
    • Code:
      -- Example displaying the full weekday name
      SELECT DATE_FORMAT(NOW(), '%W') AS weekday_name;
      
  6. Formatting Hours and Minutes with DATE_FORMAT in MySQL:

    • Description: Use %H for 24-hour format, %h for 12-hour format, %i for minutes, and %p for AM/PM.
    • Code:
      -- 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;
      
  7. Changing the Order of Day, Month, and Year with DATE_FORMAT:

    • Description: Adjust the order of components by rearranging the format specifiers.
    • Code:
      -- Example changing the order of day, month, and year
      SELECT DATE_FORMAT(NOW(), '%m-%d-%Y') AS formatted_date;
      
  8. MySQL DATE_FORMAT for Formatting Date Ranges:

    • Description: Apply DATE_FORMAT to both the start and end dates to format a date range.
    • Code:
      -- 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;
      
  9. Formatting Dates with Leading Zeros in MySQL:

    • Description: Use %d with %m to ensure day and month have leading zeros when needed.
    • Code:
      -- Example formatting dates with leading zeros
      SELECT DATE_FORMAT(NOW(), '%d/%m/%Y') AS formatted_date;