MySQL Date and Time Functions: DATE_ADD, DATEDIFF, DATE_FORMAT

MySQL 60 min min read Updated: Nov 27, 2026 Intermediate
MySQL Date and Time Functions: DATE_ADD, DATEDIFF, DATE_FORMAT
Intermediate Topic 3 of 3

Current Date and Time

CURDATE(), NOW() and CURRENT_TIMESTAMP give the present moment. Their results change every second, so our examples below use fixed dates. Just to see them work:

sql SELECT CURDATE() AS today, NOW() AS right_now, CURTIME() AS time_now;

Pulling Out Parts

sql SELECT order_date, YEAR(order_date) AS yr, MONTH(order_date) AS mon, DAY(order_date) AS dy, DAYNAME(order_date) AS weekday, MONTHNAME(order_date) AS month_name, QUARTER(order_date) AS qtr FROM orders WHERE id IN (1, 6) ORDER BY id;

Result:

order_dateyrmondyweekdaymonth_nameqtr
2025-01-102025110FridayJanuary1
2025-04-02202542WednesdayApril2

EXTRACT(part FROM date) is the standard SQL way:

sql SELECT EXTRACT(YEAR_MONTH FROM '2025-03-15') AS year_month_value, EXTRACT(WEEK FROM '2025-03-15') AS week_no, DAYOFWEEK('2025-03-15') AS dow_sun1, WEEKDAY('2025-03-15') AS weekday_mon0;

Result:

year_month_valueweek_nodow_sun1weekday_mon0
2025031075
Two weekday numbers DAYOFWEEK() returns 1 for Sunday to 7 for Saturday. WEEKDAY() returns 0 for Monday to 6 for Sunday. Check which one your code expects.

Adding and Subtracting Time

sql SELECT DATE_ADD('2025-01-31', INTERVAL 1 MONTH) AS plus_month, DATE_ADD('2025-03-15', INTERVAL 10 DAY) AS plus_10_days, DATE_SUB('2025-03-15', INTERVAL 1 YEAR) AS minus_year, '2025-03-15' + INTERVAL 2 WEEK AS plus_2_weeks;

Result:

plus_monthplus_10_daysminus_yearplus_2_weeks
2025-02-282025-03-252024-03-152025-03-29

Notice 2025-01-31 + 1 month becomes 2025-02-28: MySQL clamps to the last valid day of the month.

Differences Between Dates

sql SELECT DATEDIFF('2025-03-20', '2025-03-15') AS days_between, TIMESTAMPDIFF(MONTH, '2025-01-15', '2025-04-14') AS full_months, TIMESTAMPDIFF(YEAR, '1990-06-15', '2025-03-15') AS age_years, TIMESTAMPDIFF(HOUR, '2025-03-15 08:00:00', '2025-03-15 17:30:00') AS hours;

Result:

days_betweenfull_monthsage_yearshours
52349

DATEDIFF(later, earlier) gives days. TIMESTAMPDIFF(unit, earlier, later) gives whole units, with the earlier date first. Mixing up the argument order is a classic bug: DATEDIFF takes the later date first.

Formatting: DATE_FORMAT

CodeMeaningCodeMeaning
%Y4-digit year%y2-digit year
%mMonth 01-12%cMonth 1-12
%dDay 01-31%eDay 1-31
%MMonth name%bShort month
%WWeekday name%aShort weekday
%HHour 00-23%iMinutes
%sSeconds%pAM or PM
sql SELECT DATE_FORMAT('2025-03-05 14:07:09', '%d %b %Y') AS pretty, DATE_FORMAT('2025-03-05 14:07:09', '%W, %M %e') AS long_form, DATE_FORMAT('2025-03-05 14:07:09', '%H:%i') AS clock, DATE_FORMAT('2025-03-05', '%Y-%m') AS year_month_text;

Result:

prettylong_formclockyear_month_text
05 Mar 2025Wednesday, March 514:072025-03

Parsing Text into Dates: STR_TO_DATE

STR_TO_DATE(text, format) is the reverse of DATE_FORMAT. Use it for non-ISO input such as 15/03/2025:

sql SELECT STR_TO_DATE('15/03/2025', '%d/%m/%Y') AS parsed, STR_TO_DATE('March 5, 2025', '%M %e, %Y') AS parsed_text, STR_TO_DATE('31/02/2025', '%d/%m/%Y') AS impossible_date;

Result:

parsedparsed_textimpossible_date
2025-03-152025-03-05NULL

An impossible date returns NULL (and a warning) from STR_TO_DATE.

Month Boundaries

sql SELECT LAST_DAY('2025-02-10') AS month_end, DATE_FORMAT('2025-02-10', '%Y-%m-01') AS month_start, DATE_SUB(LAST_DAY('2025-02-10'), INTERVAL DAY(LAST_DAY('2025-02-10')) - 1 DAY) AS month_start_again, DAYOFYEAR('2025-12-31') AS day_of_year;

Result:

month_endmonth_startmonth_start_againday_of_year
2025-02-282025-02-012025-02-01365

Date Ranges the Right Way

Use a half-open range on the bare column so an index can be used and timestamps with a time part work:

sql SELECT id, order_date FROM orders WHERE order_date >= '2025-04-01' AND order_date < '2025-05-01' ORDER BY id;

Result:

idorder_date
62025-04-02
72025-04-20

Group by month without functions that hide the column in WHERE:

sql SELECT DATE_FORMAT(order_date, '%Y-%m') AS month, COUNT(*) AS orders FROM orders GROUP BY DATE_FORMAT(order_date, '%Y-%m') ORDER BY month;

Result:

monthorders
2025-011
2025-022
2025-032
2025-042
2025-052
2025-061

DATE, DATETIME and Time Zones

A TIMESTAMP column is stored in UTC and converted to the session time zone when read. A DATETIME is stored exactly as written. Let us prove it with two columns holding the same moment, read in two time zones:

sql CREATE TABLE zone_demo (ts TIMESTAMP, dt DATETIME); SET time_zone = '+00:00'; INSERT INTO zone_demo VALUES ('2025-03-15 12:00:00', '2025-03-15 12:00:00'); SELECT 'UTC session' AS session_zone, ts, dt FROM zone_demo; SET time_zone = '+05:30'; SELECT 'IST session' AS session_zone, ts, dt FROM zone_demo;

Result:

session_zonetsdt
UTC session2025-03-15 12:00:002025-03-15 12:00:00

Result:

session_zonetsdt
IST session2025-03-15 17:30:002025-03-15 12:00:00

The TIMESTAMP moved from 12:00 to 17:30 when the session zone changed to +05:30, while the DATETIME stayed at 12:00. Neither is wrong, but you must know which one you chose.

Rule of thumb Store moments in UTC and convert in the application. Use DATE for calendar days that have no time zone, such as birthdays.

Common Beginner Mistakes

MistakeWhat happensFix
Swapping DATEDIFF argumentsNegative numbersDATEDIFF(later, earlier)
BETWEEN on a DATETIME end dateMisses the last day's times>= start AND < next_day
Using YEAR(col) = 2025 in WHEREIndex not usedUse a range
Mixing time zone expectationsOff-by-hours dataStore UTC
Storing dates as textNo functions, bad sortingUse DATE types
Assuming + 1 MONTH keeps the dayJan 31 becomes Feb 28Be aware of month ends

Practice

  1. Show each order with the weekday name.
  2. Calculate how many days after joining each customer placed their first order.
  3. Show the last day of the month of every order date.
  4. Parse the text '07-04-2025' as day-month-year with STR_TO_DATE.

Conclusion

Use DATE_ADD, DATEDIFF, TIMESTAMPDIFF, DATE_FORMAT and STR_TO_DATE for date work, filter with half-open ranges on the bare column, and store moments in UTC. Know the difference between DATETIME and TIMESTAMP.

Quick Summary: DATE_ADD, DATEDIFF, TIMESTAMPDIFF, DATE_FORMAT, STR_TO_DATE, LAST_DAY, ranges, time zones.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators