by aherrick
5. September 2008 04:21
I was working on my Twitter extension to display my most recent tweet and I wanted to display how long ago I tweeted. I was able to acquire some code which I tweaked a bit to fit my needs. Check it out!
This method accepts two dates that you want to compare. For example: In my case, date1 is "DateTime.Now" and date2 is the Tweet date (whenever my most recent tweet was). I will have a full post on the twitter extension you see above soon!
Take a look at the code below!
private string GetDifferenceDate(DateTime date1, DateTime date2)
{
if (DateTime.Compare(date1, date2) >= 0)
{
TimeSpan ts = date1.Subtract(date2);
// if more than 1 day ago
if (ts.Days > 0)
return string.Format("{0} days, {1} hours, {2} minutes",
ts.Days, ts.Hours, ts.Minutes);
else
return string.Format("{0} hours, {1} minutes", ts.Hours, ts.Minutes);
}
else
return "Not valid";
}
3cf90c33-742a-49a9-9367-4ae46a9d97bc|1|4.0
Tags: c#