Date - Today date from last year

I am looking for today date from last year. This provoides  '01/01/2014'  Dim year As New DateTime(DateTime.Now.Year - 1, 1, 1) , how can I get 3/24/2014 (today date from last year) ?
VBdotnet2005Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russ SuterCommented:
DateTime.Now.AddYears(-1)
0
Russ SuterCommented:
The only issue you might run into is if you happen to call it on February 29th. In that case the return value would be February 28th of the previous year. Just something to be aware of.
0
käµfm³d 👽Commented:
*No Points*

Also be aware that you have to capture the result of Russ Suter's suggestion--it is not "in-place".

e.g.

DateTime result = DateTime.Now.AddYears(-1);

Open in new window

0
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

VBdotnet2005Author Commented:
Sorry, If you don't mind. How can I get the last date of previous year?
0
louisfrCommented:
Last date of previous year ?
New DateTime(DateTime.Now.Year - 1, 12, 31)

Open in new window

0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Another option:
        Dim LastDateOfLastYear As DateTime = (New DateTime(DateTime.Today.Year, 1, 1)).AddDays(-1)

Open in new window

0
Gustav BrockCIOCommented:
You should always use ToDay if you have no use for the time part. And you asked for VB, thus:
Dim DateLastYear As DateTime = DateTime.Today.AddYears(-1)

Open in new window

If ToDay is Feb. 29th, the result has to be Feb. 28th so this should not present an issue.

If today is Feb. 28th of a year following a leap year, the resulting date will also be Feb. 28th even though the last day of Feb. - the ultimo date - is the 29th. This could in some cases represent an issue.

/gustav
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
@Gustav

You should always use ToDay if you have no use for the time part.
Why is that? Today doesn't do any magic other than setting the time component of a DateTime to midnight. So what is the concern?
0
Gustav BrockCIOCommented:
Well, the question was:

> how can I get 3/24/2014 (today date from last year) ?

It explicitly mentions a date. A date is a date, not a date + time.

Wether it is important if the time part is included or not depends, of course, of your application and how you will query the data. The most important consequence of including the time part is, that if you wish to compare date values by the the date, you cannot use the date as is but will have to take steps to strip the time part.

I mostly program accounting and line-of-business applications so I'm, in a way, raised with the importance and influences of this matter.

/gustav
0
käµfm³d 👽Commented:
you cannot use the date as is but will have to take steps to strip the time part.
As I mentioned, using Today does not "strip the time part;" it merely sets it to midnight. While I do agree with your reasoning, really all it comes down to is ensuring that both time parts are set to the same time value--which Today accomplishes quite succinctly. All DateTime's in .NET have a time value. There is no date-only type in .NET.

I'm not saying that Today shouldn't be used, rather I'm just trying to clarify what what Today is doing and why it works.
0
Gustav BrockCIOCommented:
True, but you raised the question:  

> So what is the concern?

and that was, that the question was about a date.
My comment was not about the data type.

/gustav
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.