Solved

Get last year date and the year before

Posted on 2015-01-05
4
164 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]
Comment Utility
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
Comment Utility
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
Comment Utility
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 32

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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!
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

12 Experts available now in Live!

Get 1:1 Help Now