Reply to comment
Format a DateTime as a Facebook-style date with Months, Days, Hours, Minutes Ago
When you post a status update on Facebook, instead of showing you the exact date/time when you posted, the default view of Facebook is to show you the amount of minutes, hours, days, and even months since the post occurred similar to this screenshot from Facebook.

There are several benefits to this. 1) This is usually a more user friendly way to present dates to users. 2) It doesn't require a user to have to mentally calculate how old a post is. 3) You don't have to deal with time zones with your displayed dates because as long as you use a constant date/timezone to calculate from, you will always see the correct number of hours since.
Here is a simple method to display a DateTime in this format. Notice how it uses ranges for how to display the final text to the user. For instance, if the post is greater than 24 hours, then it shows it daily terms, etc.
/// <summary> /// Formats the date specified as a string noting how long ago /// the date occurred in text (similar to the way Facebook shows dates) /// instead of showing the actual date. NOTE: dates are calculated from /// Universal Time (UTC). /// </summary> /// <param name="oDate">The date to show how long ago as a string /// that it occurred.</param> public static string FormatDateAgo(object oDate) { DateTime date = Convert.ToDateTime(oDate); DateTime dateNow = DateTime.UtcNow; if (dateNow >= date) { if (dateNow.Year != date.Year) { // get number of months since posting int iMonthCount = 0; DateTime dateIndex = date; while (dateIndex <= dateNow) { iMonthCount++; dateIndex = dateIndex.AddMonths(1); } if (iMonthCount > 11) { return "Over a year ago"; } else if (iMonthCount == 1) { return "1 month ago"; } else { return iMonthCount.ToString() + " months ago"; } } int iMonthDiff = dateNow.Month - date.Month; if (iMonthDiff > 0) { if (iMonthDiff == 1) { return "1 month ago"; } return iMonthDiff.ToString() + " months ago"; } TimeSpan ts = dateNow.Subtract(date); if (ts.Days > 0) { if (ts.Days == 1) { return "1 day ago"; } return ts.Days.ToString() + " days ago"; } if (ts.Hours > 0) { if (ts.Hours == 1) { return "1 hour ago"; } return ts.Hours.ToString() + " hours ago"; } if (ts.Minutes > 0) { if (ts.Minutes == 1) { return "1 minute ago"; } return ts.Minutes.ToString() + " minutes ago"; } } return "0 minutes ago"; }
| Attachment | Size |
|---|---|
| daysago1.png | 7.8 KB |
Reply
Popular Articles
Last viewed:
- Windows Server 2003 Configure RRAS (Routing and Remote Access Service) Site to Site VPN
- How to Slipstream Windows XP Service Pack 3 Installation
- C# Store/Retrieve File in Database Image Field using ODBC
- Oracle Functions Tips
- Using Stored Procedures in the Entity Framework with Scalar Return Values
- Disable Button on Submit (Prevent multiple submits)

Recent comments
5 days 10 hours ago
1 week 1 day ago
1 week 1 day ago
2 weeks 4 hours ago
2 weeks 1 day ago
2 weeks 2 days ago
2 weeks 4 days ago
2 weeks 5 days ago
3 weeks 16 hours ago
3 weeks 1 day ago