Solved

Get last year date and the year before

Posted on 2015-01-05
4
165 Views
Last Modified: 2015-01-08
How can I get last previous date year and the previous date year before when I pass value from a combobox?
Sample  combobox values  2015, 2014, 2013, etc
If current year only, pass 2015 I expect to get
dim current_year as datetime = 1/5/2015
dim previous_year as datetime = 1/5/2014

if pass 2014
dim current_year as datetime = 12/31/2014
dim previous_year as datetime = 12/31/2013

if pass 2013
dim current_year as datetime = 12/31/2013
dim previous_year as datetime = 12/31/2012

etc

This is what I have so far.

current_targetDate = new-datetime(ASPxComboBox_currentYear.Value, datetime.today.month, datetime.today.day)
previous_targetDate = new-datetime(ASPxComboBox_currentYear.Value -1 , datetime.today.month, datetime.today.day)

Open in new window

0
Comment
Question by:VBdotnet2005
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40532966
so, you have different behavior for "current year" and for "other years", right?

the question is, where exactly is the problem?
if the currentyear value is the same as datetime.today.year, you do as you showed, and for all the other years, you use 12/31 for month and day...
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 40533015
try this

Dim day, month As Integer
        Dim current_targetDate, previous_targetDate As Date
        day = 31
        month = 12
        If (ASPxComboBox_currentYear.SelectedValue = DateTime.Today.Year) Then
            day = DateTime.Today.Day
            month = DateTime.Today.Month

        End If
        current_targetDate = New DateTime(ASPxComboBox_currentYear.SelectedValue, month, day)
        previous_targetDate = New DateTime(ASPxComboBox_currentYear.SelectedValue - 1, month, day)
        Response.Write(current_targetDate.ToShortDateString())
        Response.Write(previous_targetDate.ToShortDateString())
    End Sub

Open in new window

0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40533069
Yes, something like this:
	Dim current_targetDate	As DateTime
	Dim previous_targetDate As DateTime
	Dim selected_year 		As Integer

	selected_year = ASPxComboBox_currentYear.Value
	If selected_year = Date.Today.Year Then
		current_targetDate = Date.Today
	Else
		current_targetDate = New Datetime(selected_year, 12, 31)
	End If
	previous_targetDate = Microsoft.VisualBasic.DateAdd("yyyy", -1, current_targetDate)

	Debug.Print(previous_targetDate.ToString() & " - " & current_targetDate.ToString())

Open in new window

This will not fail for a date of Februry 29th.
/gustav
0
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40533612
Are you looking for something like this:
Imports System.Globalization

Module Module1
	Sub Main()
		Dim calculatedDate() = CalculateDate("2013")
		Console.WriteLine("2013 Current Date: {0:MM/dd/yyyy}", calculatedDate(0))
		Console.WriteLine("2013 Previous Date: {0:MM/dd/yyyy}", calculatedDate(1))
		Console.WriteLine()
		calculatedDate = CalculateDate("2014")
		Console.WriteLine("2014 Current Date: {0:MM/dd/yyyy}", calculatedDate(0))
		Console.WriteLine("2014 Previous Date: {0:MM/dd/yyyy}", calculatedDate(1))
		Console.WriteLine()
		calculatedDate = CalculateDate("2015")
		Console.WriteLine("2015 Current Date: {0:MM/dd/yyyy}", calculatedDate(0))
		Console.WriteLine("2015 Previous Date: {0:MM/dd/yyyy}", calculatedDate(1))
		Console.ReadLine()
	End Sub

	Function CalculateDate(ByVal enteredDate As String) As DateTime()
		Dim currentDate As DateTime = DateTime.Now()
		Dim calculatedDate As DateTime
		Dim results(2) As DateTime
		If DateTime.TryParseExact(enteredDate, "yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, calculatedDate) Then
			If calculatedDate.Year <> currentDate.Year AndAlso calculatedDate.AddMonths(-1).Year <> currentDate.Year Then
				currentDate = New DateTime(calculatedDate.Year, calculatedDate.AddMonths(-1).Month, 1).AddMonths(1).AddDays(-1)
			End If
			results(0) = currentDate
			results(1) = currentDate.AddYears(-1)
		End If
		Return results
	End Function
End Module

Open in new window

Produces the following output -Capture.JPG
-saige-
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now