Boundaries Crossed vs Time Passed
Concepts covered: Date Differences
A user signs up at 2024-01-31 23:00:00 and places a first order at 2024-02-01 01:00:00. Between those moments 2 hours passed, and over the same span the calendar crossed 1 midnight and 1 month boundary. Each count is correct for its own question, and a report has to choose which question it is asking.
| User 1's 2 hours | Days | Months |
|---|---|---|
| Complete units of time passed | 0 | 0 |
| Calendar boundaries crossed | 1 | 1 |
What date_diff counts
date_diff(unit, start, end) measures time passed. It counts how many complete units fit between the 2 values and drops whatever is left over. On timestamps a day means 24 full hours, so 23:00 one evening to 01:00 the next morning is 0 days. A DATE has no time of day, so between 2 dates each whole day passed is also 1 midnight crossed. Cast both sides to DATE and the same call counts calendar days.
Swap the 2 arguments and the sign flips. date_diff counts from its second argument to its third, so an end earlier than its start returns a negative number.
| user_id | signed_up_at | first_order_at |
|---|---|---|
| 1 | 2024-01-31 23:00:00 | 2024-02-01 01:00:00 |
| 2 | 2024-01-15 09:00:00 | 2024-02-14 09:00:00 |
| 3 | 2024-01-31 10:00:00 | 2024-02-29 10:00:00 |
| 4 | 2024-03-01 00:00:00 | 2024-03-31 23:59:59 |
Days passed
Counts complete 24-hour days. User 1 waited 2 hours, so the answer is 0.
SELECT
user_id,
date_diff('day', signed_up_at, first_order_at) AS days_passed
FROM signups| user_id | days_passed |
|---|---|
| 1 | 0 |
| 2 | 30 |
| 3 | 29 |
| 4 | 30 |
Midnights
Casting to DATE first counts calendar days. User 1 now reads 1, the midnight between January 31 and February 1.
SELECT
user_id,
date_diff('day', CAST(signed_up_at AS DATE), CAST(first_order_at AS DATE)) AS midnights_crossed
FROM signups| user_id | midnights_crossed |
|---|---|
| 1 | 1 |
| 2 | 30 |
| 3 | 29 |
| 4 | 30 |
Months
Complete months passed beside month boundaries crossed. Users 1 and 2 crossed into February without completing a month.
SELECT
user_id,
date_diff('month', signed_up_at, first_order_at) AS months_passed,
(EXTRACT(YEAR FROM first_order_at) - EXTRACT(YEAR FROM signed_up_at)) * 12
+ EXTRACT(MONTH FROM first_order_at)
- EXTRACT(MONTH FROM signed_up_at) AS month_boundaries
FROM signups| user_id | months_passed | month_boundaries |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 0 | 1 |
| 3 | 1 | 1 |
| 4 | 0 | 0 |
User 3 shows another rule of date_diff. January 31 to February 29 counts as 1 complete month, because a start on day 31 completes its month on the last day of a shorter month.
Counting month and year boundaries
For months, date_diff offers no boundary count, so build one with EXTRACT. Turn each value into a running month number with year * 12 + month and subtract one from the other. January 2024 becomes 24289 and February 2024 becomes 24290, so any 2 moments in those months differ by exactly 1. The day and hour don't matter. Years need less: the difference of the 2 years counts year boundaries crossed.
The split shows up at every size. From 10:59 to 11:01, date_diff('hour', ...) returns 0 while the clock moved into a new hour, and from December 31 to January 1, date_diff('year', ...) returns 0 while the calendar moved into a new year.
Pick the question first
- Time passed: date_diff on timestamps
- A delivery that took 3 hours or more
- A subscription that has run 6 complete months
- A session shorter than 30 minutes
- A refund window of 14 full days
- Boundaries crossed: DATE casts or part arithmetic
- Days since last login, as the user's calendar shows it
- Cohort month index: joined January, active February is month 1
- Nights between a rental's check-in and checkout
- Orders this calendar year against last calendar year
Retention reports show what the wrong choice costs. For a January cohort, month 1 is supposed to mean February. With date_diff('month', ...), a user who joined on January 20 and came back on February 10 lands in month 0 beside the people active in January, and month 1 shows fewer returning users than really returned.
Age goes the other way. Someone born on March 16, 1990 is still 33 on March 15, 2024, because age counts complete years passed. Counting year boundaries would make them 34 a day early, as customer 2 shows.
SELECT
customer_id,
birth_date,
date_diff('year', birth_date, DATE '2024-03-15') AS age,
year(DATE '2024-03-15') - year(birth_date) AS years_crossed
FROM customers| customer_id | birth_date |
|---|---|
| 1 | 1990-03-14 |
| 2 | 1990-03-16 |
| 3 | 2000-02-29 |
| customer_id | birth_date | age | years_crossed |
|---|---|---|---|
| 1 | 1990-03-14 | 34 | 34 |
| 2 | 1990-03-16 | 33 | 34 |
| 3 | 2000-02-29 | 24 | 24 |
One row has placed_at = 2024-03-14 22:00:00 and delivered_at = 2024-03-16 06:00:00. What are a and b?
SELECT
date_diff('day', placed_at, delivered_at) AS a,
date_diff('day', CAST(placed_at AS DATE), CAST(delivered_at AS DATE)) AS b
FROM deliveries- a = 2, b = 2
- a = 1, b = 1
- a = 1, b = 2
- a = 1.33, b = 2
About this lesson
This section is part of the Dates: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.