在 MariaDB 中获取日期所在月份的名称

本文讨论如何在 MariaDB 中使用 DATE_FORMAT() 函数获取指定的日期所在月份的名称。

发布于

在 MariaDB 中,您可以使用 DATE_FORMAT() 函数返回日期所在月份的名称,可以是短名称(比如 NovDec),也可以是全名称(比如 NovemberDecember)。

获取月份短名称

在 MariaDB 中,您可以在 DATE_FORMAT() 函数中 %b 格式说明符获取指定日期所在月份的短名称,例如:

SELECT DATE_FORMAT('2022-01-01', '%b');

输出:

+---------------------------------+
| DATE_FORMAT('2022-01-01', '%b') |
+---------------------------------+
| Dec                             |
+---------------------------------+

获取月份长名称

在 MariaDB 中,您可以在 DATE_FORMAT() 函数中 %M 格式说明符获取指定日期所在月份的长名称,例如:

SELECT DATE_FORMAT('2022-01-01', '%M');

输出:

+---------------------------------+
| DATE_FORMAT('2022-01-01', '%M') |
+---------------------------------+
| December                        |
+---------------------------------+

获取当前月份的名称

下面的示例展示了如何获取当前月份的名称:

SELECT
  CURRENT_DATE,
  DATE_FORMAT(CURRENT_DATE, '%M') "Full Name",
  DATE_FORMAT(CURRENT_DATE, '%b') "Short Name";

输出:

+--------------+-----------+------------+
| CURRENT_DATE | Full Name | Short Name |
+--------------+-----------+------------+
| 2022-01-01   | December  | Dec        |
+--------------+-----------+------------+

获取所有月份的名称

下面的示例展示了如何获取获取所有月份的名称,包括长名称和段名称:

SELECT
  '2022-01-01' AS "Day",
  DATE_FORMAT('2022-01-01', '%M') AS "Full Name",
  DATE_FORMAT('2022-01-01', '%b') AS "Short Name"
UNION
SELECT
  '2022-02-01' AS "Day",
  DATE_FORMAT('2022-02-01', '%M') AS "Full Name",
  DATE_FORMAT('2022-02-01', '%b') AS "Short Name"
UNION
SELECT
  '2022-03-01' AS "Day",
  DATE_FORMAT('2022-03-01', '%M') AS "Full Name",
  DATE_FORMAT('2022-03-01', '%b') AS "Short Name"
UNION
SELECT
  '2022-04-01' AS "Day",
  DATE_FORMAT('2022-04-01', '%M') AS "Full Name",
  DATE_FORMAT('2022-04-01', '%b') AS "Short Name"
UNION
SELECT
  '2022-05-01' AS "Day",
  DATE_FORMAT('2022-05-01', '%M') AS "Full Name",
  DATE_FORMAT('2022-05-01', '%b') AS "Short Name"
UNION
SELECT
  '2022-06-01' AS "Day",
  DATE_FORMAT('2022-06-01', '%M') AS "Full Name",
  DATE_FORMAT('2022-06-01', '%b') AS "Short Name"
UNION
SELECT
  '2022-07-01' AS "Day",
  DATE_FORMAT('2022-07-01', '%M') AS "Full Name",
  DATE_FORMAT('2022-07-01', '%b') AS "Short Name"
UNION
SELECT
  '2022-08-01' AS "Day",
  DATE_FORMAT('2022-08-01', '%M') AS "Full Name",
  DATE_FORMAT('2022-08-01', '%b') AS "Short Name"
UNION
SELECT
  '2022-09-01' AS "Day",
  DATE_FORMAT('2022-09-01', '%M') AS "Full Name",
  DATE_FORMAT('2022-09-01', '%b') AS "Short Name"
UNION
SELECT
  '2022-10-01' AS "Day",
  DATE_FORMAT('2022-10-01', '%M') AS "Full Name",
  DATE_FORMAT('2022-10-01', '%b') AS "Short Name"
UNION
SELECT
  '2022-11-01' AS "Day",
  DATE_FORMAT('2022-11-01', '%M') AS "Full Name",
  DATE_FORMAT('2022-11-01', '%b') AS "Short Name"
UNION
SELECT
  '2022-12-01' AS "Day",
  DATE_FORMAT('2022-12-01', '%M') AS "Full Name",
  DATE_FORMAT('2022-12-01', '%b') AS "Short Name";

输出:

+------------+-----------+------------+
| Day        | Full Name | Short Name |
+------------+-----------+------------+
| 2022-01-01 | January   | Jan        |
| 2022-02-01 | February  | Feb        |
| 2022-03-01 | March     | Mar        |
| 2022-04-01 | April     | Apr        |
| 2022-05-01 | May       | May        |
| 2022-06-01 | June      | Jun        |
| 2022-07-01 | July      | Jul        |
| 2022-08-01 | August    | Aug        |
| 2022-09-01 | September | Sep        |
| 2022-10-01 | October   | Oct        |
| 2022-11-01 | November  | Nov        |
| 2022-12-01 | December  | Dec        |
+------------+-----------+------------+

结论

本文讨论如何在 MariaDB 中使用 DATE_FORMAT() 函数获取指定的日期是星期几:

  • %b 格式说明符获取指定日期所在月份的短名称
  • %M 格式说明符获取指定日期所在月份的长名称

您可以查看更多 MariaDB 日期格式化说明符