Home  >  Article  >  Database  >  How to use the DATE_FORMAT function in MySQL to convert dates to different formats

How to use the DATE_FORMAT function in MySQL to convert dates to different formats

WBOY
WBOYOriginal
2023-07-12 10:22:533809browse

How to use the DATE_FORMAT function in MySQL to convert dates into different formats

Date is a common data type in databases. In practical applications, we often need to format dates to meet different needs. MySQL provides the DATE_FORMAT function to convert dates into different formats.

The syntax of the DATE_FORMAT function is as follows:
DATE_FORMAT(date, format)

Among them, date is the date to be converted, and format is the specified format string. The following are some commonly used date format codes:

  • %Y: 4-digit year
  • %y: 2-digit year
  • %m: 2-digit year Month
  • %d: 2-digit date
  • %H: Hour in 24-hour format
  • %h: Hour in 12-hour format
  • %i : Minutes with 2 digits
  • %s: Seconds with 2 digits
  • %W: Full name of the week
  • %w: Abbreviation of the week
  • %M: The full name of the month
  • %b: The abbreviation of the month

Here are some examples demonstrating how to use the DATE_FORMAT function for date format conversion:

  1. Convert date to year-month-date format:

    SELECT DATE_FORMAT('2022-06-30', '%Y-%m-%d');

    The output result is: 2022-06-30

  2. Convert date to month -The format of date-year:

    SELECT DATE_FORMAT('2022-06-30', '%m-%d-%Y');

    The output result is: 06-30-2022

  3. Convert the date to the format of hours:minutes:seconds and use 24 Hour format:

    SELECT DATE_FORMAT('2022-06-30 14:30:45', '%H:%i:%s');

    The output result is: 14:30:45

  4. Convert the date to the full name of the week:

    SELECT DATE_FORMAT('2022-06-30', '%W');

    The output result is :Thursday

  5. Convert the date to the abbreviation of the month:

    SELECT DATE_FORMAT('2022-06-30', '%b');

    The output result is: Jun

##In addition to the above example In addition, we can also perform more complex date format conversion according to actual needs. For example, we can convert dates to a representation of a quarter, or convert dates to a specific format to meet specific needs.

It should be noted that the DATE_FORMAT function can only be used for format conversion and cannot be used for date calculation or comparison. If you need to calculate or compare dates, you can use other date functions provided by MySQL, such as DATE_ADD, DATEDIFF, etc.

In actual development, understanding and skillfully using the DATE_FORMAT function is one of the keys to processing date data. By rationally choosing the format string, we can convert dates into different formats suitable for display and analysis, improving the flexibility and readability of the program.

To summarize, the DATE_FORMAT function is a powerful tool in MySQL for converting dates into different formats. By mastering its use, we can meet various date formatting needs and better process and display date data.

The above is the detailed content of How to use the DATE_FORMAT function in MySQL to convert dates to different formats. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn