942,960 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 8616
  • C# RSS
Jun 1st, 2008
0

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

Expand Post »
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
art vandelay is offline Offline
5 posts
since Nov 2007
Jun 2nd, 2008
0

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

Here you are

c# Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jun 3rd, 2008
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
art vandelay is offline Offline
5 posts
since Nov 2007
Jun 4th, 2008
0

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

You're welcome, hope it helped!
Please mark your thread solved if it been.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: move cursor automatically in a richtextbox
Next Thread in C# Forum Timeline: Expression too complex





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC