Calculate "time ago" - time that passed since a DateTime

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 5
Reputation: art vandelay is an unknown quantity at this point 
Solved Threads: 0
art vandelay art vandelay is offline Offline
Newbie Poster

Calculate "time ago" - time that passed since a DateTime

 
0
  #1
Jun 1st, 2008
I've been searching like crazy for this but can't find anything in C#/VB.NET.

I want to show off a DateTime as a "x days x hours x minutes x seconds ago" string, so I'm looking for a function that calculates the time difference between a DateTime and the current date and time, and shows it in that nice string that you can see everywhere around the web these days.

Preferably something like what geekpedia.com has on its frontpage. Note that it always shows the two most relevant time objects at once. For example for the newer posting it shows "17 hours, 31 minutes ago" and for the older ones it shows "3 days, 19 hours ago" -- it doesn't keep showing minutes, seconds, unless relevant.

If anyone happens to have a C#/VB.NET function for this, it will be much appreciated.

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Calculate "time ago" - time that passed since a DateTime

 
0
  #2
Jun 2nd, 2008
Here you are

  1. static void Main(string[] args)
  2. {
  3. Console.WriteLine(GetDifferenceDate(new DateTime(2008, 11, 25, 10, 30, 2),
  4. new DateTime(2008, 6, 2, 6, 3, 5)));
  5. }
  6.  
  7. static string GetDifferenceDate(DateTime date1, DateTime date2)
  8. {
  9. if (DateTime.Compare(date1, date2) >= 0)
  10. {
  11. TimeSpan ts = date1.Subtract(date2);
  12. return string.Format("{0} days, {1} hours, {2} minutes, {3} seconds",
  13. ts.Days, ts.Hours, ts.Minutes, ts.Seconds);
  14. }
  15. else
  16. return "Not valid";
  17. }
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 5
Reputation: art vandelay is an unknown quantity at this point 
Solved Threads: 0
art vandelay art vandelay is offline Offline
Newbie Poster

Re: Calculate "time ago" - time that passed since a DateTime

 
0
  #3
Jun 3rd, 2008
Thanks a lot!

This code is so much simpler than similar functions I've seen for other languages (such as PHP.) Guess we owe it to the versatility of the DateTime class.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Calculate "time ago" - time that passed since a DateTime

 
0
  #4
Jun 4th, 2008
You're welcome, hope it helped!
Please mark your thread solved if it been.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC