Home > Database > Mysql Tutorial > 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
Release: 2023-07-12 10:22:53
Original
3906 people have browsed it

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');
    Copy after login

    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');
    Copy after login

    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');
    Copy after login

    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');
    Copy after login

    The output result is :Thursday

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

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

    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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template