Questions tagged [datediff]

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.

1423 questions
1577
votes
39 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
  • 60,897
  • 45
  • 146
  • 152
748
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
230
votes
6 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…
Alex
  • 22,845
  • 25
  • 92
  • 147
157
votes
27 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 :)
williamtroup
  • 11,291
  • 19
  • 64
  • 94
138
votes
32 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,539
  • 2
  • 12
  • 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,210
  • 17
  • 50
  • 92
106
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
  • 6,716
  • 16
  • 59
  • 99
93
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
  • 11,983
  • 14
  • 41
  • 43
92
votes
19 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,189
  • 12
  • 57
  • 84
88
votes
20 answers

The difference in months between dates in MySQL

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 the dividing by 2 592 000 (seconds) and rounding up whithin MySQL?
Darryl Hein
  • 134,677
  • 87
  • 206
  • 257
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,444
  • 6
  • 23
  • 34
73
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,305
  • 14
  • 35
  • 42
71
votes
8 answers

Difference between dates in JavaScript

How to find the difference between two dates?
Saranya
  • 1,051
  • 6
  • 15
  • 28
59
votes
7 answers

SQL Server Group By Month

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
  • 9,631
  • 27
  • 126
  • 209
55
votes
24 answers

How to calculate age in T-SQL with years, months, and days

What would be the best way to calculate someone's age in years, months, and days in T-SQL (SQL Server 2000)? The datediff function doesn't handle year boundaries well, plus getting the months and days separate will be a bear. I know I can do it on…
Barry
  • 2,043
  • 3
  • 17
  • 28
1
2 3
94 95