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

F

Family Tree Mike

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...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top