Languages
[Edit]
EN

MySQL - DATEDIFF() function example

0 points
Created by:
Abul-Emery
457

In this article, we would like to show you DATEDIFF() function example in MySQL.

Quick solution:

-- SELECT DATEDIFF('to_date','from_date');
SELECT DATEDIFF('YYYY-MM-DD','YYYY-MM-DD');

or 

-- SELECT DATEDIFF('to_date','from_date');
SELECT DATEDIFF('YYYY-MM-DD hh:mm:ss','YYYY-MM-DD hh:mm:ss');

Where:

  • YYYY - four-digit year format, e.g. 2021,
  • MM - month number (counted from 01 to 12),
  • DD - day number (from 01 to 31 - depending on month),
  • hh - hour number (from 00 to 24),
  • mm - minute number (from 00 to 59),
  • ss - second number (from 00 to 59).

 

DATEDIFF() function returns the number of days between two date or datetime values.

Note:

It doesn't take time (HH:MM:SS) into account.

Practical example

In this example, we will calculate the difference between two dates using DATEDIFF() function.

Queries:

SELECT DATEDIFF('2021-01-08','2021-01-05') AS 'data difference';
SELECT DATEDIFF('2021-01-08 09:35:27','2021-01-05 11:22:55') AS 'data difference';

Result:

MySQL - DATEDIFF() function example - result
MySQL - DATEDIFF() function example - result

Note:

Notice that both of the above queries give the same result (difference between 2021-01-08 and 2021-01-05 - 3 days). They don't take time into account.

Related posts

MySQL - dates

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join