Open In App

DATEDIFF() Function in MySQL

Last Updated : 25 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

DATEDIFF() function in MySQL is used to return the number of days between two specified date values. 

Syntax:

DATEDIFF(date1, date2)

Parameter: This function accepts two parameters as given below:

  • date1: First specified date
  • date2: Second specified date

Returns :

It returns the number of days between two specified date values.

Example 1 :

Getting the number of days between two specified date values where the date is specified in the format of YYYY-MM-DD. Here the date1 is greater than date2, so the return value is positive.

SELECT DATEDIFF("2020-11-20", "2020-11-1");  

Output :

19

Example 2:

Getting the number of days between two specified date values where the date is specified in the format of YYYY-MM-DD. Here the date1 is less than date2, so the return value is negative.

SELECT DATEDIFF("2020-11-12", "2020-11-19");  

Output:

-7

Example 3:

Getting the number of days between two specified date values where the date is specified in the format of YYYY-MM-DD HH-MM-SS. 

SELECT DATEDIFF("2020-11-20 09:34:21", "2020-11-17 09:34:21");  

Output:

3

Example 4:

Getting the number of days between two specified date values where the date is specified in the format of YYYY-MM-DD HH-MM-SS. Here time value does not matter as date1 and date2 are taken the same but time is different still the output is zero (0).

SELECT DATEDIFF("2020-11-20 09:34:21", "2020-11-20 08:11:23");  

Output:

0


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads