DATE_FORMAT(date, format) turns a DATE, DATETIME or TIMESTAMP value into a string using format specifiers such as %Y for the four-digit year, %m for the month number and %d for the day. It is the standard way to produce readable dates like 17 Nov 2026 or 2026-11 directly in a MySQL query.
This guide explains the common specifiers, shows report-style examples, and covers the reverse function STR_TO_DATE(). Examples work on MySQL 8.4 LTS and 9.x.
The first argument is a date or datetime value, and the second is a format string made of literal text and specifiers that start with %.
The result is always a string, not a date. If you need to keep sorting or comparing as dates, keep the original column in ORDER BY and WHERE, and use DATE_FORMAT() only in the SELECT list.
If the date argument is NULL or invalid, the function returns NULL.
These are the specifiers you will use most often. They are case-sensitive, so %m and %M mean different things.
| Specifier | Meaning | Example for 2026-11-07 09:05:03 |
|---|---|---|
%Y | Four-digit year | 2026 |
%y | Two-digit year | 26 |
%m | Month number, zero padded | 11 |
%c | Month number, no padding | 11 |
%M | Full month name | November |
%b | Short month name | Nov |
%d | Day of month, zero padded | 07 |
%e | Day of month, no padding | 7 |
%D | Day with English suffix | 7th |
%W | Full weekday name | Saturday |
%a | Short weekday name | Sat |
%H | Hour, 24-hour clock | 09 |
%h or %I | Hour, 12-hour clock | 09 |
%i | Minutes | 05 |
%s | Seconds | 03 |
%p | AM or PM | AM |
To print a literal percent sign, write %%. Other text, such as spaces, commas and dashes, is copied as it is.
Here are patterns that appear in invoices, dashboards and exports. They all use the same timestamp.
Use %H:%i:%s for logs and APIs, and %h:%i %p for people. For machine-to-machine data, it is usually better to return the raw DATETIME or an ISO 8601 string and let the client format it for the user's locale.
DATE_FORMAT() is useful for grouping rows into periods. Given an orders table with ordered_at and total columns:
Because %Y-%m sorts correctly as text (year first, padded month), the ORDER BY month result is chronological. If you used %b %Y, Apr 2026 would sort before Jan 2026, which is a classic reporting bug. When you format for display, sort by the formatted key only if it sorts naturally, or sort by the original date column.
On a large table, grouping by a function on the column cannot use an index for the grouping. For heavy reports, consider a generated column such as order_month CHAR(7) AS (DATE_FORMAT(ordered_at, '%Y-%m')) STORED with an index.
The names produced by %M, %b, %W and %a depend on the session variable lc_time_names. The default is en_US.
The setting affects only the names, not the order of the parts, so you still choose the order in the format string. It lasts for the session unless you reset it.
STR_TO_DATE(str, format) is the inverse. It reads a string using the same specifiers and returns a DATE, DATETIME or TIME value, depending on which parts the format contains.
If the string does not match the format, the function returns NULL and, in strict SQL mode, a warning appears when you use it in data changes. Use it when importing text in a non-standard layout, then store a real date column.
| Function | Use |
|---|---|
TIME_FORMAT(time, format) | Formats a time value with the time specifiers such as %H, %i and %s |
DATE(dt) | Strips the time part and keeps a real DATE |
EXTRACT(unit FROM d) | Returns a single numeric part, such as the year |
DAYNAME(d), MONTHNAME(d) | Return weekday or month names without a format string |
%m is the numeric month with a leading zero, such as 03. %M is the full month name, such as March.
A string. Do not use the result for date arithmetic or range filters. Keep using the original column for those.
Use DATE_FORMAT(col, '%d/%m/%Y'). Store the value as a DATE and format it only when you display it.
Use %h:%i %p, for example DATE_FORMAT(col, '%h:%i %p'). For a 24-hour clock, use %H:%i.
DATE_FORMAT() converts dates to strings with specifiers like %Y, %m, %d, %H and %i, where case matters. Format only for display, group and sort by %Y-%m style keys so ordering stays chronological, use lc_time_names for localised names, and use STR_TO_DATE() to parse text back into a real date.