Re: format dateTime '06/08/2003 12:00' to '1 day ago'

  • Thread starter Carl Prothman [MVP]
  • Start date
C

Carl Prothman [MVP]

Ted said:
I would like to format my date "06/08/2003 12:00" to something like "1 day
ago" or "24 hours ago". Is there a date format I can use to do this or does
anyone have any pointers as to how i could do this.

Ted,
You can subtract a day from a DateTime by using it's Subtract
method and a TimeSpan of 24 hours.
e.g.
Dim dt As DateTime = DateTime.Now()
dt = dt.Subtract(New TimeSpan(24, 0, 0))
Dim s As String = dt.ToShortDateString()

--

Thanks,
Carl Prothman
Microsoft ASP.NET MVP
http://www.able-consulting.com
 
K

Kit George [MSFT]

Cindy, its worth picking up a book to discover what 'new' means, since
theres a lot of related concepts you probably need to know.

The short answer is, new will make a new instance of a given type. 'New
TimeSpan' makes a timespan. The reason you don't see this on say the
DateTime line, is because DateTime.Now() is a function which does the new on
your behalf.

DateDiff works fine by the way, but dealing with objects can allow a bit
more flexibility if you're interested.

Kit
 
C

Cindy Meister -WordMVP-

Hi Kit,
Cindy, its worth picking up a book to discover what 'new' means, since
theres a lot of related concepts you probably need to know.
I'm doing that :) But you know
- books don't cover everything
- one has to start somewhere, and I chose the ground floor
- three more books in the pile to go after I get through the current 1000+
pages; maybe it will be in one of them!

Thanks, the answer does actually make some sense :) I wouldn't know what to
do with it "in situ" - yet. But I can follow what you're saying.
The short answer is, new will make a new instance of a given type. 'New
TimeSpan' makes a timespan. The reason you don't see this on say the
DateTime line, is because DateTime.Now() is a function which does the new on
your behalf.

DateDiff works fine by the way, but dealing with objects can allow a bit
more flexibility if you're interested.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister
http://www.mvps.org/word
http://go.compuserve.com/MSOfficeForum

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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

Forum statistics

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

Latest Threads

Top