7 Replies - 2045 Views - Last Post: 03 April 2009 - 01:59 PM Rate Topic: -----

#1 marinabl   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 01-November 08

My date method is off

Post icon  Posted 30 March 2009 - 10:38 PM

Hello,
I am trying to calculate the number of years for my calculate age event handler, however i am apparently missing a method or whatnot because im trying to calculate the age , can anyone suggest help on this. I need to subtract years to get my age but when i try the birthdate.Subtract (currentDate) method i get an error about not able to convert from LONG??? So below is what I have.
Thank You

 
Private Sub btnCalculateAge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateAge.Click
		Dim currentDate As Date
		Dim BirthDate As Date
		Dim Age As Long

		If IsDate(txtBirthDate.Text) Then
			BirthDate = CDate(txtBirthDate.Text)
			Age = DateDiff(DateInterval.Year, BirthDate, currentDate)

			MessageBox.Show("Current Date: " & currentDate.ToShortDateString & _
						   vbCrLf & "Birth Date: " & BirthDate.ToShortDateString & vbCrLf & _
							"Age " & Age.ToString, " Age Calculation")
		End If
	End Sub


End Class

Is This A Good Question/Topic? 0
  • +

Replies To: My date method is off

#2 searockruz   User is offline

  • D.I.C Regular

Reputation: 24
  • View blog
  • Posts: 460
  • Joined: 07-March 09

Re: My date method is off

Posted 30 March 2009 - 11:00 PM

try this,although i am not sure
Dim dt As DateTime 
Dim dt1 As DateTime 

dt = CType(dateTimePicker1.Value.Date,DateTime) 
dt1 = CType(dateTimePicker2.Value.Date,DateTime) 
Dim span As TimeSpan = dt1.Subtract(dt) 
label1.Text = span.Days.ToString() 


Was This Post Helpful? 0
  • +
  • -

#3 marinabl   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 01-November 08

Re: My date method is off

Posted 31 March 2009 - 09:11 AM

Thanks for the try that did not work. I just need to know what property or method im using that is wrong
Was This Post Helpful? 0
  • +
  • -

#4 myork   User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • Posts: 129
  • Joined: 04-March 09

Re: My date method is off

Posted 31 March 2009 - 09:29 AM

There's likely a more direct way, but the date.subtract() returns a timespan, which can be used like this:
		Dim dt As Date = Today()
		Dim dt2 As Date = Today().AddYears(3)
		Dim span As TimeSpan = dt2.Subtract(dt)
		MsgBox(CInt(span.TotalDays / 365).ToString)

Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: My date method is off

Posted 31 March 2009 - 09:34 AM

How about this?
  Dim Birthday As Date
  If Date.TryParse(txtBirthDate.Text, Birthday) = False Then
   MessageBox.Show("I can't recognise that as a date.")
  Else
   Dim Age As TimeSpan = Now.Subtract(Birthday)
   MessageBox.Show(String.Format("You are {0} Years old.", Age.Days / 365.25))
  End If


Was This Post Helpful? 0
  • +
  • -

#6 marinabl   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 01-November 08

Re: My date method is off

Posted 02 April 2009 - 01:19 PM

That was the approach that I originally used to resolve this problem, but I kept getting an error about the variable age hiding another variable this was within the subtract statement?? :-(
Was This Post Helpful? 0
  • +
  • -

#7 dbasnett   User is offline

  • D.I.C Addict
  • member icon

Reputation: 124
  • View blog
  • Posts: 686
  • Joined: 01-October 08

Re: My date method is off

Posted 03 April 2009 - 05:55 AM

This will give you the age in years and days
	Private Function AgeCalc(ByVal DatePast As DateTime, _
								Optional ByVal DateFuture As DateTime = Nothing) As String
		'calculate age using two dates. the age will be years and days
		'USE:
		'Dim ageSTR() As String
		'ageSTR = AgeCalc(#2/29/1008#, #2/28/2009#).Split("|"c)
		'Debug.WriteLine(ageSTR(0)) 'years
		'Debug.WriteLine(ageSTR(1)) 'days
		'Debug.WriteLine(ageSTR(2)) 'how long calculation took (in ticks)
		'
		'note - the wonderful thing about DateSerial is that it takes care of Leap Birthdays AKA leaplings
		'a leaplings b-day is 3/1 for non-leap years
		Dim dp, df, nxtAnniv As DateTime, retval As String, ts As New TimeSpan
		Dim years, days As Integer, stpw As New Stopwatch
		stpw.Start() 'provide metric
		If DateFuture = Nothing Then DateFuture = DateTime.Now 'use now for future date
		If DatePast > DateFuture Then 'make sure the past is the past
			dp = DateFuture
			df = DatePast
		Else
			dp = DatePast
			df = DateFuture
		End If
		years = df.Year - dp.Year 'calculate years
		'create next "birthday"
		'nxtAnniv = New System.DateTime(df.Year, dp.Month, dp.Day) 'does not take care of leap b-day
		nxtAnniv = DateSerial(df.Year, dp.Month, dp.Day) 'takes care of leap b-day
		If nxtAnniv > df Then 'is the next "birthday" > future date
			years -= 1 'yes, adjust year
			nxtAnniv = DateSerial(df.Year - 1, dp.Month, dp.Day) 're-create so it is before future
		End If
		ts = df - nxtAnniv 'will give days
		days = ts.Days
		retval = years.ToString & "|" & days.ToString   'create return value
		stpw.Stop() 'how long did it take?
		retval &= "|" & stpw.ElapsedTicks.ToString
		Return retval
	End Function


Was This Post Helpful? 0
  • +
  • -

#8 dlkirk03   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 12-September 08

Re: My date method is off

Posted 03 April 2009 - 01:59 PM

Try this code I didn't test it.

Private Sub btnCalculateAge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateAge.Click
		Dim currentDate As Integer = Date.Now.Year.ToString
		Dim BirthDate As Date
		Dim BirVar As Integer
		Dim Age As Long

		If IsDate(txtBirthDate.Text) Then
			BirthDate = CDate(txtBirthDate.Text)
			BirVar = BirthDate.Year.ToString
			Age = currentDate - BirVar
			MessageBox.Show("Current Date: " & Date.Now.ToString("MM/dd/yyy") & _
						   vbCrLf & "Birth Date: " & BirthDate.ToShortDateString & vbCrLf & _
							"Age " & Age.ToString, " Age Calculation")
		End If
	End Sub


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1