• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 452
  • Last Modified:

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) ?
0
VBdotnet2005
Asked:
VBdotnet2005
  • 3
  • 3
  • 2
  • +3
4 Solutions
 
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
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 TomlinsonMiddle School Assistant TeacherCommented:
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now