Home All Groups Group Topic Archive Search About

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

Author
25 Jun 2009 12:27 PM
Nully Girl
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%20Basic/27-1.aspx

Author
25 Jun 2009 12:52 PM
Mark Rae [MVP]
"Nully Girl" <nullyg***@hotmail.com> wrote in message
news:O3b2hCZ9JHA.4376@TK2MSFTNGP04.phx.gbl...

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

public string StringFromDate(DateTime MyDateTime)
{
    string MyString = String.Empty;

    // do some processing on MyDateTime;

    return MyString
}


> C#
> http://www
>
> VB.NET
> http://www

Is this a genuine question, or are you just spamming the Microsoft
Technology People website...?


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Are all your drivers up to date? click for free checkup

Author
25 Jun 2009 6:40 PM
Family Tree Mike
Show quote Hide quote
"Nully Girl" wrote:

> 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%20Basic/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...
}

Bookmark and Share