Solved

Get last year date and the year before

Posted on 2015-01-05
4
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 143

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 50

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 34

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

724 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