C# / VB.NET code which takes a datetime in as parameter and returnsa string

  • Thread starter Thread starter Nully Girl
  • Start date Start date
Nully Girl said:
Hi,

I am wondering if someone know where I can find some code which takes a
datetime as a parameter and returns a string like many of the forums
example of some return strings..

2 seconds ago
3 minutes ago
3 minutes and 20 seconds ago
4 hours 3 minutes ago
3 days ago
.....
.....
06/01/2009


C#
http://www.hd720i.com/Category/CSharp/16-1.aspx

VB.NET
http://www.hd720i.com/Category/Visual Basic/27-1.aspx

There may be a package already written to do this, but you could do:

string ElapsedTime(DateTime basetime)
{
string when;
TimeSpan ts;
DateTime t = DateTime.Now;

if (basetime < t)
{
when = "ago";
ts = t.Subtract(basetime);
}
else
{
when = "from now";
ts = basetime.Subtract(t);
}

if (ts.Days > 0)
return string.Format("{0} days {1} hours {2}", ts.Days, ts.Hours, when);
else if (ts.Hours > 0)
return string.Format("{0} hours {1} minutes {2}", ts.Hours, ts.Minutes,
when);

// etc...
}
 
Back
Top