Temporal Analytics: NOW, CURDATE, DATE_FORMAT, STR_TO_DATE, DATEDIFF & DATE_ADD in MySQL
Overview & Learning Objectives
Temporal analysis drives essential business metrics: Month-over-Month (MoM) revenue pacing, customer churn periods, and delivery SLA breaches. Working with dates in SQL requires mastering temporal parsing, formatting, and arithmetic. In MySQL, analysts rely on `DATE_FORMAT()` to extract month/year periods, `STR_TO_DATE()` to ingest dirty date strings, `DATEDIFF()` and `TIMESTAMPDIFF()` to measure durations, and `DATE_ADD()` for forward projections.
- Extract calendar intervals and format executive date strings using `DATE_FORMAT()`.
- Parse custom date strings (e.g. "13/11/2026") into valid MySQL dates using `STR_TO_DATE()`.
- Calculate delivery durations and customer age using `DATEDIFF()` and `TIMESTAMPDIFF()`.
- Execute date arithmetic using `DATE_ADD(col, INTERVAL n DAY)`.
Industry Standard
Standard: Analytics pipelines at Uber and DoorDash compute exact delivery SLA durations in integer minutes using `TIMESTAMPDIFF(MINUTE, pickup_time, drop_time)` rather than naive date subtraction.
Real-World Example: ScoopCraft Dark-Store Delivery SLA Performance
ScoopCraft tracks delivery timestamps from order placement to door delivery to flag SLA breaches (> 25 minutes).
| order_id | order_time | delivered_time | Duration in Min (TIMESTAMPDIFF) | SLA Status |
|---|---|---|---|---|
| ORD-801 | 2026-11-13 14:02:10 | 2026-11-13 14:18:45 | 16 minutes | Within SLA (< 25 min) |
| ORD-802 | 2026-11-13 14:10:00 | 2026-11-13 14:38:20 | 28 minutes | Breached SLA (+3 min) |
| ORD-803 | 2026-11-13 14:15:30 | 2026-11-13 14:34:10 | 18 minutes | Within SLA |
Functions & Syntax Covered in this Chapter
Here is the definition, execution syntax, parameter breakdown, and real-world business example for each function/syntax covered in this chapter:
NOW(), CURDATE() & CURTIME()
Definition: Returns the current datetime, current date, or current time according to the server's active timezone.
SELECT CURDATE() AS today, NOW() AS current_datetime;
NOW()DATE_FORMAT(date, format)
Definition: Formats a date or datetime value according to specifier tokens (%Y=4-digit year, %m=month, %d=day, %H=24-hr, %i=min).
SELECT DATE_FORMAT(order_date, "%Y-%m") AS sales_month, DATE_FORMAT(order_date, "%W, %M %e, %Y") FROM orders;
format: Format string using MySQL date specifiers (%Y, %m, %d, %H, %i, %s, %W, %M).
DATE_FORMAT(order_date, '%Y-%m')DATEDIFF(expr1, expr2) & TIMESTAMPDIFF(unit, expr1, expr2)
Definition: DATEDIFF returns days between two dates (expr1 - expr2). TIMESTAMPDIFF computes exact intervals (MINUTE, HOUR, DAY, MONTH, YEAR) between expr1 and expr2 (expr2 - expr1).
SELECT DATEDIFF(CURDATE(), signup_date) AS days_active, TIMESTAMPDIFF(MINUTE, order_time, delivery_time) AS delivery_min FROM orders;
unit: In TIMESTAMPDIFF: SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR.expr1, expr2: Datetime expressions.
TIMESTAMPDIFF(MINUTE, dispatch_time, delivered_time)DATE_ADD() & DATE_SUB()
Definition: Adds or subtracts specified time intervals (INTERVAL n DAY/MONTH/HOUR) to/from a date or datetime.
SELECT DATE_ADD(CURDATE(), INTERVAL 30 DAY) AS expiry_date, DATE_SUB(NOW(), INTERVAL 7 DAY) AS week_ago;
INTERVAL n unit: Specifies interval magnitude and unit (e.g. INTERVAL 1 MONTH).
DATE_ADD(purchase_date, INTERVAL 3 MONTH)Common Mistakes & Limitations
- Writing `DATEDIFF(order_time, delivered_time)` and getting negative numbers because DATEDIFF evaluates (t1 - t2).
- Using naive date subtraction (`date1 - date2`) in MySQL, which performs numeric integer subtraction on formatted numbers rather than true calendar math.
Keyboard Shortcuts (MySQL Workbench)
| Action | Windows | Mac |
|---|---|---|
| Execute Query | Ctrl + Enter | Cmd + Enter |
Pro Tips & Modern Best Practices
- To group by month, always use `DATE_FORMAT(date, "%Y-%m")` or `LAST_DAY(date)` to ensure proper sorting across multi-year trends.
- Use `NOW()` for full timestamps (including time) and `CURDATE()` when you only need midnight dates without time components.