MySQL Dates
Dates and times are some of the most common values in a database: when a customer signed up, when an order was placed, when it shipped. MySQL has dedicated types for them, and you should always use those rather than storing dates as text.
Date and time types
| Type | Stores | Format | Range |
|---|---|---|---|
DATE |
a calendar day | 'YYYY-MM-DD' |
1000-01-01 to 9999-12-31 |
DATETIME |
date and time of day | 'YYYY-MM-DD hh:mm:ss' |
year 1000 to 9999 |
TIMESTAMP |
a moment in time, stored as UTC | 'YYYY-MM-DD hh:mm:ss' |
1970-01-01 to 2038-01-19 |
TIME |
a time of day or a duration | 'hh:mm:ss' |
-838:59:59 to 838:59:59 |
YEAR |
a year | YYYY |
1901 to 2155 |
DATETIME, TIMESTAMP and TIME accept fractional seconds: DATETIME(3) keeps milliseconds, DATETIME(6) microseconds.
CREATE TABLE deliveries (
delivery_id INT AUTO_INCREMENT PRIMARY KEY,
planned_day DATE,
departed_at DATETIME,
logged_at TIMESTAMP NULL,
slot_start TIME,
fiscal_year YEAR
);
INSERT INTO deliveries (planned_day, departed_at, logged_at, slot_start, fiscal_year)
VALUES ('2025-03-09', '2025-03-09 07:45:00', '2025-03-09 07:45:30', '08:00', 2025);
SELECT planned_day, departed_at, logged_at, slot_start, fiscal_year FROM deliveries;+-------------+---------------------+---------------------+------------+-------------+
| planned_day | departed_at | logged_at | slot_start | fiscal_year |
+-------------+---------------------+---------------------+------------+-------------+
| 2025-03-09 | 2025-03-09 07:45:00 | 2025-03-09 07:45:30 | 08:00:00 | 2025 |
+-------------+---------------------+---------------------+------------+-------------+
DATETIME or TIMESTAMP?
Both look the same, but they behave differently:
TIMESTAMPconverts from the connection's time zone to UTC when storing and back when reading. Two users in different time zones see the same moment in their own local time. It ends in January 2038.DATETIMEstores exactly what you give it, with no time zone at all, and reaches the year 9999.
A common convention is to store all times in UTC in DATETIME columns and convert in the application, or to use TIMESTAMP for "when did this happen" audit columns such as created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP.
Writing date values
Write dates as strings in ISO order, 'YYYY-MM-DD'. MySQL converts them automatically when it compares them with a date column. To be explicit, use a typed literal (DATE '2025-03-09') or CAST, and to read other formats use STR_TO_DATE with a format string:
SELECT DATE '2025-03-09' AS literal,
CAST('2025-03-09 18:30' AS DATETIME) AS cast_datetime,
STR_TO_DATE('09/03/2025', '%d/%m/%Y') AS from_european,
STR_TO_DATE('March 9, 2025', '%M %e, %Y') AS from_us_text;+------------+---------------------+---------------+--------------+
| literal | cast_datetime | from_european | from_us_text |
+------------+---------------------+---------------+--------------+
| 2025-03-09 | 2025-03-09 18:30:00 | 2025-03-09 | 2025-03-09 |
+------------+---------------------+---------------+--------------+
An impossible date, like February 30, is rejected: CAST('2025-02-30' AS DATE) returns NULL, and inserting it into a DATE column fails in strict mode (error 1292, Incorrect date value).
Comparing and filtering dates
Dates compare in time order, so <, >, BETWEEN and ORDER BY just work:
SELECT order_id, order_date, shipped_date
FROM orders
WHERE order_date >= '2025-04-01' AND order_date < '2025-05-01'
ORDER BY order_date;+----------+------------+--------------+
| order_id | order_date | shipped_date |
+----------+------------+--------------+
| 13 | 2025-04-01 | 2025-04-02 |
| 14 | 2025-04-08 | 2025-04-10 |
| 15 | 2025-04-16 | 2025-04-18 |
| 16 | 2025-04-25 | NULL |
+----------+------------+--------------+
The half-open range >= first day AND < first day of next month is the most robust way to select a month. It works for DATETIME columns too, and it can use an index on the column. WHERE MONTH(order_date) = 4 gives the same rows here, but can't use an index and would also match April of every other year.
Date arithmetic
Add or subtract intervals with + INTERVAL n unit (or DATE_ADD/DATE_SUB), and measure differences with DATEDIFF (in days) or TIMESTAMPDIFF(unit, start, end):
SELECT order_id, order_date, shipped_date,
DATEDIFF(shipped_date, order_date) AS days_to_ship,
order_date + INTERVAL 30 DAY AS return_deadline
FROM orders
WHERE order_id IN (1, 7, 12, 20);+----------+------------+--------------+--------------+-----------------+
| order_id | order_date | shipped_date | days_to_ship | return_deadline |
+----------+------------+--------------+--------------+-----------------+
| 1 | 2025-01-05 | 2025-01-07 | 2 | 2025-02-04 |
| 7 | 2025-02-14 | 2025-02-18 | 4 | 2025-03-16 |
| 12 | 2025-03-21 | 2025-03-24 | 3 | 2025-04-20 |
| 20 | 2025-05-28 | NULL | NULL | 2025-06-27 |
+----------+------------+--------------+--------------+-----------------+
Order 20 hasn't shipped, so its shipped_date and everything computed from it are NULL.
Month arithmetic clamps to the end of the month instead of overflowing:
SELECT DATE '2025-01-31' + INTERVAL 1 MONTH AS jan31_plus_month,
DATE '2024-02-29' + INTERVAL 1 YEAR AS leap_plus_year,
TIMESTAMPDIFF(MONTH, '2024-01-15', '2025-03-14') AS full_months,
TIMESTAMPDIFF(YEAR, '2019-03-01', '2025-02-28') AS full_years;+------------------+----------------+-------------+------------+
| jan31_plus_month | leap_plus_year | full_months | full_years |
+------------------+----------------+-------------+------------+
| 2025-02-28 | 2025-02-28 | 13 | 5 |
+------------------+----------------+-------------+------------+
TIMESTAMPDIFF counts only complete units: 13 full months, because the 14th month would end on March 15. That is exactly what you want for computing ages or years of service.
Tip:
CURDATE(),NOW()andCURRENT_TIMESTAMPreturn the current date and time. They're essential in real applications (WHERE order_date >= CURDATE() - INTERVAL 7 DAY), but this course uses fixed dates so that every example prints the same result every time. The Date Functions lesson lists many more functions.
Test yourself
Which type converts values to UTC for storage and back to the session time zone when read?
- DATE
- DATETIME
- TIMESTAMP
- TIME
What is DATE '2025-01-31' + INTERVAL 1 MONTH?
- 2025-02-31
- 2025-03-03
- 2025-02-28
- NULL
Which WHERE clause best selects all of April 2025 in a DATETIME column?
- col = '2025-04'
- col BETWEEN '2025-04-01' AND '2025-04-30'
- col >= '2025-04-01' AND col < '2025-05-01'
- col LIKE '2025-04'