Solved

Get last year date and the year before

Posted on 2015-01-05
4
168 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help for captcha 2 40
Make borderless form movable by user 2 25
Adding Rows to Grid 4 29
function that i need to run every 1 min in win form app 6 38
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

737 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