Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get last year date and the year before

Posted on 2015-01-05
4
Medium Priority
?
171 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 52

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 2000 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

610 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