MySQL's DATE_FORMAT function usage

In MySQL, you can use the DATE_FORMAT function to format a date into the desired format. The DATE_FORMAT function accepts two parameters: date and format string.

Here are some common date formatting options:

  • %Y: four-digit year (for example: 2023)
  • %y: two-digit year (for example: 23)
  • %m: two-digit month (01 to 12)
  • %c: month (1 to 12)
  • %d: two-digit date (01 to 31)
  • %e: date (1 to 31)
  • %H: Hour number in 24-hour format (00 to 23)
  • %h: hour in 12-hour format (01 to 12)
  • %i: Minutes (00 to 59)
  • %s: seconds (00 to 59)
  • %p: AM or PM
    Here are some examples of how to use the DATE_FORMAT function for date formatting:

Format the date into "YYYY-MM-DD" format:

SELECT DATE_FORMAT(date_column, '%Y-%m-%d') FROM table_name;

Format the date into "MM/DD/YYYY" format:

SELECT DATE_FORMAT(date_column, '%m/%d/%Y') FROM table_name;

Format the date into "MM, DD, YYYY" format:

SELECT DATE_FORMAT(date_column, '%Y年%m月%d日') FROM table_name;

Please replace "date_column" in the above example with the name of the date column you want to format, and "table_name" with the name of the table.

Guess you like

Origin blog.csdn.net/qq_22744093/article/details/134395758