Home All Groups Group Topic Archive Search About

Date and Time Calculation.

Author
28 Jul 2006 9:08 PM
YardDancer
Dear All,

I am upgrading my skills from VB 6.0 to VB.Net.  They say you can do all
date and time calculations using the  properties and methods of DateTime
structure withour recourse to the functions and properties of the  VB
runtime library. (oh  yeah !!!)

I have been trying to determine the year interval between two dates using
the properties and methods of the DateTime structure so that it is
consistent with the .Net Framework.

I know how to this using the properties and functions of the VB run time
library that inclues the Microsoft VisualBasic namespace.

E.g.  number of years = DateDiff(DateInterval.Year, dtmBirthDate,
dtmTodaysDate)

So the question is how do i get the number of years "accurately" using the
properties and methods of DataTime structure given that

date.subtract(date) = timespan  and there seems to be no direct way to
extract the year  value from TimeSpan.

date.subtract(timespan) = date , but  i dont see how  i would get my year
from this.

Can anybody help me with this PRETTY PLEASE

Yard Dancer

Author
28 Jul 2006 10:06 PM
Mythran
Show quote Hide quote
"YardDancer" <dancer_y***@hotmail.com> wrote in message
news:%23$IdLoosGHA.4596@TK2MSFTNGP04.phx.gbl...
> Dear All,
>
> I am upgrading my skills from VB 6.0 to VB.Net.  They say you can do all
> date and time calculations using the  properties and methods of DateTime
> structure withour recourse to the functions and properties of the  VB
> runtime library. (oh  yeah !!!)
>
> I have been trying to determine the year interval between two dates using
> the properties and methods of the DateTime structure so that it is
> consistent with the .Net Framework.
>
> I know how to this using the properties and functions of the VB run time
> library that inclues the Microsoft VisualBasic namespace.
>
> E.g.  number of years = DateDiff(DateInterval.Year, dtmBirthDate,
> dtmTodaysDate)
>
> So the question is how do i get the number of years "accurately" using the
> properties and methods of DataTime structure given that
>
> date.subtract(date) = timespan  and there seems to be no direct way to
> extract the year  value from TimeSpan.
>
> date.subtract(timespan) = date , but  i dont see how  i would get my year
> from this.
>
> Can anybody help me with this PRETTY PLEASE
>
> Yard Dancer
>

Dim fromDate As DateTime = New DateTime(2000, 3, 15)
Dim toDate As DateTime = DateTime.Now


If you want just the number of years two dates span, you just need to
subtract the two DateTime.Years.

Dim totalYears As Integer = toDate.Year - fromDate.Year



If you want the number of years, calculated by the total number of days...it
can get a little more complex.  If you don't care about leap years and
stuff, you could use:

Dim totalYears As Integer = toDate.Subtract(fromDate).Days / 356

HTH,
Mythran
Author
28 Jul 2006 11:24 PM
YardDancer
Thanks Mythran for your reply!

I have found out that the best way is

number of years = (((date1.subtract(date2)).Days) / 365.25)

namely     timespan.Days / 365.25


Thanks for your effort though

Yard Dancer
lAge = (((dtmTodaysDate.Subtract(dtmBirthDate)).Days) / 365.25)

Show quoteHide quote
"Mythran" <kip_pot***@hotmail.com> wrote in message
news:%23WRq3qpsGHA.3952@TK2MSFTNGP03.phx.gbl...
>
> "YardDancer" <dancer_y***@hotmail.com> wrote in message
> news:%23$IdLoosGHA.4596@TK2MSFTNGP04.phx.gbl...
>> Dear All,
>>
>> I am upgrading my skills from VB 6.0 to VB.Net.  They say you can do all
>> date and time calculations using the  properties and methods of DateTime
>> structure withour recourse to the functions and properties of the  VB
>> runtime library. (oh  yeah !!!)
>>
>> I have been trying to determine the year interval between two dates using
>> the properties and methods of the DateTime structure so that it is
>> consistent with the .Net Framework.
>>
>> I know how to this using the properties and functions of the VB run time
>> library that inclues the Microsoft VisualBasic namespace.
>>
>> E.g.  number of years = DateDiff(DateInterval.Year, dtmBirthDate,
>> dtmTodaysDate)
>>
>> So the question is how do i get the number of years "accurately" using
>> the properties and methods of DataTime structure given that
>>
>> date.subtract(date) = timespan  and there seems to be no direct way to
>> extract the year  value from TimeSpan.
>>
>> date.subtract(timespan) = date , but  i dont see how  i would get my year
>> from this.
>>
>> Can anybody help me with this PRETTY PLEASE
>>
>> Yard Dancer
>>
>
> Dim fromDate As DateTime = New DateTime(2000, 3, 15)
> Dim toDate As DateTime = DateTime.Now
>
>
> If you want just the number of years two dates span, you just need to
> subtract the two DateTime.Years.
>
> Dim totalYears As Integer = toDate.Year - fromDate.Year
>
>
>
> If you want the number of years, calculated by the total number of
> days...it can get a little more complex.  If you don't care about leap
> years and stuff, you could use:
>
> Dim totalYears As Integer = toDate.Subtract(fromDate).Days / 356
>
> HTH,
> Mythran
>
>
Author
28 Jul 2006 11:55 PM
Mythran
Show quote Hide quote
"YardDancer" <dancer_y***@hotmail.com> wrote in message
news:eDiKE0psGHA.4080@TK2MSFTNGP03.phx.gbl...
> Thanks Mythran for your reply!
>
> I have found out that the best way is
>
> number of years = (((date1.subtract(date2)).Days) / 365.25)
>
> namely     timespan.Days / 365.25
>
>
> Thanks for your effort though
>
> Yard Dancer
> lAge = (((dtmTodaysDate.Subtract(dtmBirthDate)).Days) / 365.25)
>

Now, if you are trying to find an age of a person, that's
different...slightly...try the following method:

Private Function GetAge(ByVal StartDate As DateTime) As Integer
    Dim today As DateTime = DateTime.Today

    If today < StartDate
        Throw New ArgumentException( _
            "The StartDate must be before today's date.", _
            "StartDate" _
        )
    End If

    Dim totalYears As Integer = today.Year - StartDate.Year

    If today.Month < StartDate.Month OrElse ( _
        today.Month = StartDate.Month AndAlso _
        today.Day < StartDate.Day _
    )
        ' Offset by a year if it has not been a full year since the last
        ' birth date.
        totalYears -= 1
    End If

    Return totalYears
End Function

Note, I haven't really tested the logic out but I believe it's right :)
Double check it to make sure...

This calculates a person's (or objects) age, in years, and returns the Years
lapsed, but not including the current year if the date (Month and Day) has
not been reached yet.  For example, my irl birthdate is August 10th.  The
GetAge method will return 25 years instead of 26 as the method you chose to
use would.  I'm 25 right now, not 26.  I will be 26 soon, but not quite
there yet :)

Got it?

HTH,
Mythran
Author
29 Jul 2006 10:08 AM
YardDancer
Now that is what i call "ACCURATE"

Thanks Mythran

Yard Dancer

Show quoteHide quote
"Mythran" <kip_pot***@hotmail.com> wrote in message
news:uDL3%23EqsGHA.4140@TK2MSFTNGP06.phx.gbl...
>
> "YardDancer" <dancer_y***@hotmail.com> wrote in message
> news:eDiKE0psGHA.4080@TK2MSFTNGP03.phx.gbl...
>> Thanks Mythran for your reply!
>>
>> I have found out that the best way is
>>
>> number of years = (((date1.subtract(date2)).Days) / 365.25)
>>
>> namely     timespan.Days / 365.25
>>
>>
>> Thanks for your effort though
>>
>> Yard Dancer
>> lAge = (((dtmTodaysDate.Subtract(dtmBirthDate)).Days) / 365.25)
>>
>
> Now, if you are trying to find an age of a person, that's
> different...slightly...try the following method:
>
> Private Function GetAge(ByVal StartDate As DateTime) As Integer
>    Dim today As DateTime = DateTime.Today
>
>    If today < StartDate
>        Throw New ArgumentException( _
>            "The StartDate must be before today's date.", _
>            "StartDate" _
>        )
>    End If
>
>    Dim totalYears As Integer = today.Year - StartDate.Year
>
>    If today.Month < StartDate.Month OrElse ( _
>        today.Month = StartDate.Month AndAlso _
>        today.Day < StartDate.Day _
>    )
>        ' Offset by a year if it has not been a full year since the last
>        ' birth date.
>        totalYears -= 1
>    End If
>
>    Return totalYears
> End Function
>
> Note, I haven't really tested the logic out but I believe it's right :)
> Double check it to make sure...
>
> This calculates a person's (or objects) age, in years, and returns the
> Years lapsed, but not including the current year if the date (Month and
> Day) has not been reached yet.  For example, my irl birthdate is August
> 10th.  The GetAge method will return 25 years instead of 26 as the method
> you chose to use would.  I'm 25 right now, not 26.  I will be 26 soon, but
> not quite there yet :)
>
> Got it?
>
> HTH,
> Mythran
>
>