Date-difference will give difference in the number of days, months, years etc. between any two calendar dates. Not defined by ISO/ANSI SQL, but several vendors have similar implementations.
Questions tagged [datediff]
1599 questions
1647
votes
41 answers
Calculate relative time in C#
Given a specific DateTime value, how do I display relative time, like:
2 hours ago
3 days ago
a month ago

Jeff Atwood
- 63,320
- 48
- 150
- 153
818
votes
34 answers
How to calculate the difference between two dates using PHP?
I have two dates of the form:
Start Date: 2007-03-24
End Date: 2009-06-26
Now I need to find the difference between these two in the following form:
2 years, 3 months and 2 days
How can I do this in PHP?
gnanesh
288
votes
7 answers
How do I check the difference, in seconds, between two dates?
There has to be an easier way to do this. I have objects that want to be refreshed every so often, so I want to record when they were created, check against the current timestamp, and refresh as necessary.
datetime.datetime has proven to be…

alexgolec
- 26,898
- 33
- 107
- 159
220
votes
29 answers
Difference in Months between two dates in JavaScript
How would I work out the difference for two Date() objects in JavaScript, while only return the number of months in the difference?
Any help would be great :)

William Troup
- 12,739
- 21
- 70
- 98
147
votes
33 answers
How to calculate "time ago" in Java?
In Ruby on Rails, there is a feature that allows you to take any Date and print out how "long ago" it was.
For example:
8 minutes ago
8 hours ago
8 days ago
8 months ago
8 years ago
Is there an easy way to do this in Java?

jts
- 1,629
- 2
- 13
- 10
129
votes
2 answers
Why does the difference between 30 March and 1 March 2020 erroneously give 28 days instead of 29?
TimeUnit.DAYS.convert(
Math.abs(
new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").parse("30-03-2020 00:00:00").getTime() -
new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").parse("1-03-2020 00:00:00").getTime()
),
…

Joe
- 7,749
- 19
- 60
- 110
117
votes
6 answers
Difference between two DateTimes C#?
I need a function that can return the difference between the below two dates as 24.
DateTime a = new DateTime(2008, 01, 02, 06, 30, 00);
DateTime b = new DateTime(2008, 01, 03, 06, 30, 00);

abmv
- 7,042
- 17
- 62
- 100
112
votes
21 answers
How to get difference between two dates in months using MySQL query?
I'm looking to calculate the number of months between 2 date time fields.
Is there a better way than getting the Unix timestamp and then dividing by 2 592 000 (seconds) and rounding up within MySQL?

Darryl Hein
- 142,451
- 95
- 218
- 261
104
votes
18 answers
Date difference in years using C#
How can I calculate date difference between two dates in years?
For example: (Datetime.Now.Today() - 11/03/2007) in years.

msbyuva
- 3,467
- 13
- 63
- 87
96
votes
3 answers
Date Difference in php on days?
Is there a quick way to calculate date difference in php? For example:
$date1 = '2009-11-12 12:09:08';
$date2 = '2009-12-01 08:20:11';
And then do a calculation, $date2 minus $date1
I read php.net documentation, but no luck. Is there a quick way to…

mysqllearner
- 13,523
- 15
- 43
- 43
81
votes
19 answers
Difference in days between two dates in Java?
I need to find the number of days between two dates: one is from a report and one is the current date. My snippet:
int age=calculateDifference(agingDate, today);
Here calculateDifference is a private method, agingDate and today are Date objects,…

Venkat
- 2,604
- 6
- 26
- 36
79
votes
8 answers
Difference between dates in JavaScript
How to find the difference between two dates?

Saranya
- 1,131
- 6
- 16
- 28
76
votes
6 answers
How to calculate DATE Difference in PostgreSQL?
Here I need to calculate the difference of the two dates in the PostgreSQL.
In SQL Server: Like we do in SQL Server its much easier.
DATEDIFF(Day, MIN(joindate), MAX(joindate)) AS DateDifference;
My Try: I am trying using the following…

Sarfaraz Makandar
- 5,933
- 16
- 59
- 84
74
votes
11 answers
How to compare two dates to find time difference in SQL Server 2005, date manipulation
I have two columns:
job_start job_end
2011-11-02 12:20:37.247 2011-11-02 13:35:14.613
How would it be possible using T-SQL to find the raw amount of time that has passed between when the job started and when the…

some_bloody_fool
- 4,605
- 14
- 37
- 46
71
votes
7 answers
How to group by month using SQL Server?
I have a table which has this schema
ItemID UserID Year IsPaid PaymentDate Amount
1 1 2009 0 2009-11-01 300
2 1 2009 0 2009-12-01 342
3 1 2010 0 …

Echilon
- 10,064
- 33
- 131
- 217