| | |
Calculate "time ago" - time that passed since a DateTime
Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
Join Date: Nov 2007
Posts: 5
Reputation:
Solved Threads: 0
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!
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!
Here you are
c# Syntax (Toggle Plain Text)
static void Main(string[] args) { Console.WriteLine(GetDifferenceDate(new DateTime(2008, 11, 25, 10, 30, 2), new DateTime(2008, 6, 2, 6, 3, 5))); } static string GetDifferenceDate(DateTime date1, DateTime date2) { if (DateTime.Compare(date1, date2) >= 0) { TimeSpan ts = date1.Subtract(date2); return string.Format("{0} days, {1} hours, {2} minutes, {3} seconds", ts.Days, ts.Hours, ts.Minutes, ts.Seconds); } else return "Not valid"; }
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
You're welcome, hope it helped!
Please mark your thread solved if it been.
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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
![]() |
Other Threads in the C# Forum
- Previous Thread: move cursor automatically in a richtextbox
- Next Thread: Expression too complex
| Thread Tools | Search this Thread |
.net access algorithm array bitmap box buttons c# check checkbox client clock combobox connection console control conversion csharp database datagrid datagridview datagridviewcheckbox dataset datetime degrees draganddrop drawing dynamiccreation encryption enum equation excel file form format formatting forms function gdi+ hospitalmanagementinformationsystem hospitalmanagementsystems httpwebrequest image input install java label list math microsystems mouseclick mysql namevaluepairs operator path photoshop picturebox pixelinversion post powerpacks programming property radians regex remoting richtextbox rows running... serialization server sleep soap socket sql sqlserver stack statistics string table teamsuite temperature text textbox thread time timer update uploadatextfile usercontrol validation visualstudio wait webbrowser whileloop windows windowsformsapplication working wpf write xml






