MySQL date functions work with the current time and with date arithmetic. NOW() returns the current date and time, DATE_ADD() adds an interval such as 7 days or 3 months to a date, and DATEDIFF() returns the number of days between two dates. These three cover most "what is due next week" and "how old is this record" questions.
The examples target MySQL 8.4 LTS and also work on 9.x. They use an orders table so you can run each statement and compare results.
NOW() returns the current date and time as a DATETIME value in the session time zone. Related functions return only part of it.
Your values will differ. CURRENT_TIMESTAMP, CURRENT_TIMESTAMP() and LOCALTIME are synonyms of NOW(). CURRENT_DATE is the same as CURDATE().
One detail matters inside a single statement: NOW() returns a constant for the whole statement, so every row sees the same value. SYSDATE() returns the actual time at each call, which makes it harder to replicate and to optimise, so prefer NOW() unless you really need the moving clock.
DATE_ADD(date, INTERVAL value unit) returns a new date. The unit can be SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER or YEAR. DATE_SUB() subtracts instead.
The operator form ordered_at + INTERVAL 1 MONTH is equivalent to DATE_ADD() and is often shorter. A useful edge case is month-end arithmetic: adding one month to January 31 gives the last day of February, because MySQL clamps to the last valid day.
Use a negative value to subtract with DATE_ADD(), for example INTERVAL -30 DAY. ADDDATE() and SUBDATE() are synonyms.
DATEDIFF(end, start) returns the number of days from start to end. It looks only at the date part and ignores the time. The result is positive when the first argument is later.
Argument order trips people up, so remember that the later date comes first. Rows where shipped_on is NULL return NULL for days_to_ship, which correctly means "not shipped yet".
When you need a unit other than days, use TIMESTAMPDIFF(unit, start, end). Here the earlier date comes first.
TIMESTAMPDIFF() returns whole units, truncating any remainder.
A common task is "orders from the last 7 days". Write the condition so MySQL can use an index on the date column.
Do not write WHERE DATE(ordered_at) = '2026-11-01' on a large table. Wrapping the column in DATE() prevents index use. Use a half-open range instead:
Half-open ranges (>= start and < next boundary) also avoid bugs with times such as 23:59:59.5.
| Function | Returns | Example |
|---|---|---|
YEAR(d), MONTH(d), DAY(d) | A part of the date | MONTH('2026-11-17') gives 11 |
DAYNAME(d) | Weekday name | DAYNAME('2026-11-17') gives Tuesday |
LAST_DAY(d) | Last day of that month | LAST_DAY('2026-02-10') gives 2026-02-28 |
DATE(dt) | Date part of a datetime | DATE(NOW()) |
EXTRACT(unit FROM d) | One unit | EXTRACT(YEAR FROM NOW()) |
Formatting a date for display is a separate topic handled by DATE_FORMAT().
NOW() follows the session time_zone variable. A TIMESTAMP column stores values in UTC and converts them to the session time zone on read, while DATETIME stores exactly what you give it. Many teams store DATETIME or TIMESTAMP in UTC and convert in the application. Check your zone with SELECT @@session.time_zone, @@global.time_zone;.
NOW() returns date and time, while CURDATE() returns only the date. Compare CURDATE() with DATE columns and NOW() with DATETIME columns.
Use DATEDIFF(later, earlier). It counts calendar days and ignores the time portion.
Use DATE_ADD(d, INTERVAL 1 MONTH) or d + INTERVAL 1 MONTH. If the day does not exist in the target month, MySQL returns the last valid day.
The arguments are in the wrong order. DATEDIFF(a, b) is a minus b, so put the later date first.
NOW() gives the current date and time, DATE_ADD() and DATE_SUB() shift dates by an interval, and DATEDIFF() returns the day difference with the later date first. Use TIMESTAMPDIFF() for hours, months or years, and filter with half-open ranges so indexes stay usable.