DATEDIFF() in MySQL — How It Works
What the MySQL DATEDIFF() function does, with an example.
Published October 19, 2026
DATEDIFF(date1, date2) returns the number of days between two dates (date1 minus date2), ignoring the time parts.
How it works
For other units use TIMESTAMPDIFF.
Try it in MySQL
SELECT DATEDIFF('2026-11-12', '2026-11-01')
11
SELECT DATEDIFF('2026-01-01', '2026-12-31')
-364
SELECT DATEDIFF(NOW(), created_at) FROM users LIMIT 1
42
Practical examples
Example 1
SELECT DATEDIFF('2026-11-12', '2026-11-01');
-- 11Example 2
SELECT DATEDIFF('2026-01-01', '2026-12-31');
-- -364Example 3
SELECT DATEDIFF(NOW(), created_at) FROM users LIMIT 1;
-- 42Use it in a real query
SELECT id, DATEDIFF('2026-11-12', '2026-11-01') AS result
FROM your_table
LIMIT 10;Common mistakes
- Swapping the argument order and getting negatives.
- Expecting hours or months.
- Passing invalid dates (returns NULL).
Key takeaways
- DATEDIFF() in one line: DATEDIFF(date1, date2) returns the number of days between two dates (date1 minus date2), ignoring the time parts.
- Try queries in a test database first.
- Check NULL handling and data types.
FAQ
Hours between?
TIMESTAMPDIFF(HOUR, a, b).
Does DATEDIFF() work in other databases?
Most have an equivalent, but names and syntax vary between MySQL, PostgreSQL, SQL Server and SQLite.