Bird
Raised Fist0
MySQLquery~5 mins

DATEDIFF and TIMESTAMPDIFF in MySQL

Choose your learning style10 modes available
Introduction

We use DATEDIFF and TIMESTAMPDIFF to find the difference between two dates or times. This helps us understand how much time passed between events.

To find how many days a project took from start to finish.
To calculate a person's age in years from their birthdate.
To check how many hours passed between two timestamps in a log.
To find the number of months between two payment dates.
To measure the difference in minutes between two event times.
Syntax
MySQL
DATEDIFF(date1, date2)

TIMESTAMPDIFF(unit, datetime1, datetime2)

DATEDIFF returns the number of days between two dates.

TIMESTAMPDIFF lets you choose the unit like SECOND, MINUTE, HOUR, DAY, MONTH, or YEAR.

Examples
This returns the number of days between June 1 and June 10, 2024.
MySQL
SELECT DATEDIFF('2024-06-10', '2024-06-01');
This returns the number of full months between January 1, 2023 and June 1, 2024.
MySQL
SELECT TIMESTAMPDIFF(MONTH, '2023-01-01', '2024-06-01');
This returns the number of hours between 8 AM and 3:30 PM on June 10, 2024.
MySQL
SELECT TIMESTAMPDIFF(HOUR, '2024-06-10 08:00:00', '2024-06-10 15:30:00');
Sample Program

This query shows how to use DATEDIFF and TIMESTAMPDIFF to find differences in days, hours, and minutes between two dates or timestamps.

MySQL
SELECT
  DATEDIFF('2024-06-15', '2024-06-10') AS days_diff,
  TIMESTAMPDIFF(DAY, '2024-06-10 08:00:00', '2024-06-15 20:00:00') AS days_diff_timestamp,
  TIMESTAMPDIFF(HOUR, '2024-06-10 08:00:00', '2024-06-10 20:00:00') AS hours_diff,
  TIMESTAMPDIFF(MINUTE, '2024-06-10 08:00:00', '2024-06-10 08:30:00') AS minutes_diff;
OutputSuccess
Important Notes

DATEDIFF only counts full days and ignores time parts.

TIMESTAMPDIFF counts full units (like full hours or full minutes) between two timestamps.

For positive results, use DATEDIFF(later, earlier) or TIMESTAMPDIFF(unit, earlier, later).

Summary

Use DATEDIFF to find days between two dates.

Use TIMESTAMPDIFF to find difference in various units like seconds, minutes, hours, days, months, or years.

Both help measure time passed between events easily.

Practice

(1/5)
1. What does the MySQL function DATEDIFF(date1, date2) return?
easy
A. The number of days between date1 and date2
B. The number of seconds between date1 and date2
C. The difference in months between date1 and date2
D. The current date and time

Solution

  1. Step 1: Understand the function purpose

    DATEDIFF calculates the difference between two dates in days only.
  2. Step 2: Confirm the output type

    The output is an integer representing how many days separate the two dates.
  3. Final Answer:

    The number of days between date1 and date2 -> Option A
  4. Quick Check:

    DATEDIFF returns days difference [OK]
Hint: DATEDIFF always returns days difference between two dates [OK]
Common Mistakes:
  • Thinking it returns seconds or months
  • Confusing order of dates for output sign
  • Using it to get current date/time
2. Which of the following is the correct syntax to find the difference in hours between two datetime values start_time and end_time using TIMESTAMPDIFF?
easy
A. TIMESTAMPDIFF(HOUR, start_time, end_time)
B. TIMESTAMPDIFF(start_time, end_time, HOUR)
C. TIMESTAMPDIFF('HOUR', end_time, start_time)
D. TIMESTAMPDIFF(end_time, start_time, 'HOUR')

Solution

  1. Step 1: Recall TIMESTAMPDIFF syntax

    The correct syntax is TIMESTAMPDIFF(unit, datetime1, datetime2) where unit is a keyword like HOUR without quotes.
  2. Step 2: Match parameters order

    Unit first, then start_time, then end_time is correct.
  3. Final Answer:

    TIMESTAMPDIFF(HOUR, start_time, end_time) -> Option A
  4. Quick Check:

    Unit first, then start, then end [OK]
Hint: TIMESTAMPDIFF(unit, start, end) with unit unquoted [OK]
Common Mistakes:
  • Putting unit last or in quotes
  • Swapping start and end parameters
  • Using lowercase unit in quotes
3. Given the query:
SELECT TIMESTAMPDIFF(DAY, '2024-01-01', '2024-01-10') AS diff_days;
What will be the output of diff_days?
medium
A. Error
B. 10
C. 11
D. 9

Solution

  1. Step 1: Understand TIMESTAMPDIFF with DAY unit

    It counts full days between the two dates, excluding the start date.
  2. Step 2: Calculate days between 2024-01-01 and 2024-01-10

    From Jan 1 to Jan 10 is 9 full days difference.
  3. Final Answer:

    9 -> Option D
  4. Quick Check:

    10 - 1 = 9 days difference [OK]
Hint: TIMESTAMPDIFF counts full units between dates [OK]
Common Mistakes:
  • Counting both start and end dates
  • Expecting 10 instead of 9
  • Confusing with DATEDIFF behavior
4. Identify the error in this query:
SELECT TIMESTAMPDIFF('MINUTE', '2024-04-01 10:00:00', '2024-04-01 11:30:00');
medium
A. TIMESTAMPDIFF cannot use MINUTE as unit
B. Dates must be swapped in order
C. Unit 'MINUTE' should not be in quotes
D. Dates must be in DATE format, not datetime

Solution

  1. Step 1: Check TIMESTAMPDIFF unit syntax

    The unit keyword like MINUTE must be unquoted, as a keyword, not a string.
  2. Step 2: Validate other parts

    The dates and order are correct; MINUTE is a valid unit; datetime format is allowed.
  3. Final Answer:

    Unit 'MINUTE' should not be in quotes -> Option C
  4. Quick Check:

    Unit keywords are unquoted [OK]
Hint: Remove quotes around unit in TIMESTAMPDIFF [OK]
Common Mistakes:
  • Quoting the unit keyword
  • Swapping date order unnecessarily
  • Thinking datetime is invalid input
5. You want to find the total number of months between two dates '2023-01-15' and '2024-06-10', but only count full months passed. Which query correctly does this?
hard
A. SELECT DATEDIFF('2024-06-10', '2023-01-15') / 30;
B. SELECT TIMESTAMPDIFF(MONTH, '2023-01-15', '2024-06-10');
C. SELECT TIMESTAMPDIFF(DAY, '2023-01-15', '2024-06-10') / 30;
D. SELECT TIMESTAMPDIFF(YEAR, '2023-01-15', '2024-06-10') * 12;

Solution

  1. Step 1: Understand requirement for full months

    We need full months difference, not approximate days divided by 30.
  2. Step 2: Check each option

    SELECT TIMESTAMPDIFF(MONTH, '2023-01-15', '2024-06-10'); uses TIMESTAMPDIFF with MONTH unit, which counts full months correctly.
    SELECT DATEDIFF('2024-06-10', '2023-01-15') / 30; and C use days divided by 30, which is approximate and can be wrong.
    SELECT TIMESTAMPDIFF(YEAR, '2023-01-15', '2024-06-10') * 12; counts years times 12, ignoring leftover months.
  3. Final Answer:

    SELECT TIMESTAMPDIFF(MONTH, '2023-01-15', '2024-06-10'); -> Option B
  4. Quick Check:

    Use TIMESTAMPDIFF with MONTH for full months [OK]
Hint: Use TIMESTAMPDIFF with MONTH for full months difference [OK]
Common Mistakes:
  • Dividing days by 30 for months
  • Using YEAR unit and multiplying
  • Using DATEDIFF which returns days only
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard